-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathindex.tsx
More file actions
125 lines (113 loc) · 3.15 KB
/
Copy pathindex.tsx
File metadata and controls
125 lines (113 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/**
* Simple Auth For Testing Only!!!
*/
import { component$ } from "@builder.io/qwik";
import {
type DocumentHead,
Form,
type RequestHandler,
globalAction$,
zod$,
} from "@builder.io/qwik-city";
import { isUserAuthenticated, signIn } from "../../../../auth/auth";
import * as z from "zod";
export const onGet: RequestHandler = async ({ redirect, cookie }) => {
if (await isUserAuthenticated(cookie)) {
throw redirect(302, "/qwikcity-test/dashboard/");
}
};
export const useSigninAction = globalAction$(
async (data, { cookie, redirect, status, fail }) => {
const result = await signIn(data, cookie);
if (result.status === "signed-in") {
throw redirect(302, "/qwikcity-test/dashboard/");
}
return fail(403, {
message: ["Invalid username or password"],
});
},
zod$(
z
.object({
username: z.string(),
password: z.string(),
confirmPassword: z.string(),
})
.superRefine(({ confirmPassword, password }, ctx) => {
if (confirmPassword !== password) {
ctx.addIssue({
code: "custom",
message: "The passwords did not match",
});
}
}),
),
);
export const useResetPasswordAction = globalAction$(
({ email }) => {
console.warn("resetPasswordAction", email);
},
zod$({
email: z.string().email(),
}),
);
export default component$(() => {
const signIn = useSigninAction();
const resetPassword = useResetPasswordAction();
return (
<div>
<h1>Sign In</h1>
<Form action={signIn} spaReset>
{signIn.value?.message && (
<p style="color:red">{signIn.value.message}</p>
)}
<label>
<span>Username</span>
<input name="username" type="text" autoComplete="username" required />
{signIn.value?.fieldErrors?.username && (
<p style="color:red">{signIn.value?.fieldErrors?.username}</p>
)}
</label>
<label>
<span>Password</span>
<input
name="password"
type="password"
autoComplete="current-password"
required
/>
{signIn.value?.fieldErrors?.password && (
<p style="color:red">{signIn.value?.fieldErrors?.password}</p>
)}
</label>
<label>
<span>Confirm password</span>
<input
name="confirmPassword"
type="password"
autoComplete="current-password"
required
/>
{signIn.value?.fieldErrors?.confirmPassword && (
<p style="color:red">
{signIn.value?.fieldErrors?.confirmPassword}
</p>
)}
</label>
<button data-test-sign-in>Sign In</button>
<p>(Username: qwik, Password: dev)</p>
</Form>
<h2>Reset Password</h2>
<form method="post" action={resetPassword.actionPath}>
<label>
<span>Email</span>
<input name="email" type="text" required />
</label>
<button data-test-reset-password>Reset Password</button>
</form>
</div>
);
});
export const head: DocumentHead = {
title: "Sign In",
};