1
1
import express from "express" ;
2
+ import { generateId } from "lucia" ;
2
3
import { Argon2id } from "oslo/password" ;
4
+ import { z } from "zod" ;
3
5
import { lucia } from "../../lib/auth.js" ;
4
- import { generateId } from "lucia" ;
5
6
import { prisma } from "../../lib/prisma.js" ;
6
- import { z } from "zod" ;
7
7
import { formatPrismaError } from "../../lib/utils.js" ;
8
8
9
9
export const signupRouter = express . Router ( ) ;
@@ -16,15 +16,17 @@ signupRouter.post("/auth/signup", async (req, res) => {
16
16
const schema = z . object ( {
17
17
email : z . string ( ) . email ( ) ,
18
18
username : z . string ( ) . min ( 3 ) . max ( 30 ) ,
19
+ name : z . string ( ) . min ( 1 ) . max ( 50 ) ,
19
20
password : z . string ( ) . min ( 6 ) . max ( 255 ) ,
21
+ persistent : z . boolean ( ) ,
20
22
} ) ;
21
23
22
24
const parsed = schema . safeParse ( req . body ) ;
23
25
if ( ! parsed . success ) {
24
26
const validationErrors = parsed . error . flatten ( ) . fieldErrors ;
25
27
return res . status ( 400 ) . json ( { error : { message : "Wrong input" , errors : validationErrors } } ) ;
26
28
}
27
- const { email, username, password } = parsed . data ;
29
+ const { email, username, name , password, persistent } = parsed . data ;
28
30
29
31
const userId = generateId ( 15 ) ;
30
32
const hashedPassword = await new Argon2id ( ) . hash ( password ) ;
@@ -35,6 +37,7 @@ signupRouter.post("/auth/signup", async (req, res) => {
35
37
id : userId ,
36
38
email,
37
39
username,
40
+ name,
38
41
hashedPassword,
39
42
} ,
40
43
} ) ;
@@ -46,5 +49,9 @@ signupRouter.post("/auth/signup", async (req, res) => {
46
49
47
50
const session = await lucia . createSession ( userId , { } ) ;
48
51
const sessionCookie = lucia . createSessionCookie ( session . id ) ;
49
- return res . appendHeader ( "Set-Cookie" , sessionCookie . serialize ( ) ) . status ( 201 ) . send ( "Success" ) ;
52
+ if ( persistent == true ) {
53
+ return res . cookie ( sessionCookie . name , sessionCookie . value , { maxAge : 7776000000 } ) . status ( 200 ) . send ( "Success" ) ;
54
+ } else {
55
+ return res . cookie ( sessionCookie . name , sessionCookie . value ) . status ( 200 ) . send ( "Success" ) ;
56
+ }
50
57
} ) ;
0 commit comments