@@ -4,7 +4,6 @@ import * as db from "../lib/db";
44import { generateTokens , sendAccessToken } from "../lib/jwt" ;
55import { validateRequest } from "../lib/middlewares" ;
66import * as z from "zod" ;
7- import generatePin from "../lib/pin" ;
87import { sendActivationEmail } from "../lib/email" ;
98
109const registerSchema = z . object ( {
@@ -47,20 +46,19 @@ router.post(
4746 }
4847 const existingUser = await db . findUserByEmail ( email ) ;
4948 if ( existingUser ) {
50- res . status ( 400 ) ;
51- throw new Error ( "Email already in use." ) ;
49+ res . status ( 409 ) ;
50+ return res . json ( { error : { message : "Email already in use." } } ) ;
5251 }
5352 const user = await db . createUserByEmailAndPassword ( { email, password } ) ;
54-
5553 //@TODO SEND EMAIL TO ACTIVATE USER
56- const pin = generatePin ( ) ;
57-
58- sendActivationEmail ( email , pin ) ;
54+ const pin = await db . createCode ( user . id ) ;
55+ console . log ( "pin" , pin ) ;
56+ await sendActivationEmail ( user , pin ) ;
5957
6058 // const { accessToken } = generateTokens(user);
6159 // sendAccessToken(res, accessToken);
6260
63- res . json ( { auth : true } ) ;
61+ return res . json ( { auth : true } ) ;
6462 } catch ( err ) {
6563 next ( err ) ;
6664 }
@@ -113,4 +111,39 @@ router.get("/logout", (req: any, res) => {
113111 return res . status ( 204 ) ;
114112} ) ;
115113
114+ router . post ( "/verify" , ( req : any , res ) => {
115+ const { uid, code } = req . body ;
116+ console . log ( "uid" , uid ) ;
117+ console . log ( "code" , code ) ;
118+ return res . json ( { uid, code } ) ;
119+ } ) ;
120+
121+ // router.get("/mail/:uid", async (req, res) => {
122+ // const uid = req.params.uid;
123+ // const pin = await db.createCode(uid);
124+ // const email = "[email protected] "; 125+ // console.log("pin", pin);
126+ // console.log("email", email);
127+ // await sendActivationEmail(email, pin);
128+ // return res.status(204);
129+ // });
130+
131+ router . get ( "/init" , ( req : any , res ) => {
132+ const user = req . user ;
133+ if ( ! user ) {
134+ return res . status ( 400 ) . json ( { error : "User and password are required." } ) ;
135+ }
136+ return res . status ( 204 ) ;
137+ } ) ;
138+
139+ router . put ( "/pwd" , async ( req : any , res ) => {
140+ const user = req . user ;
141+ const { password } = req . body ;
142+ if ( ! user || ! password ) {
143+ return res . status ( 400 ) . json ( { error : "User and password are required." } ) ;
144+ }
145+ await db . changePassword ( user . id , password ) ;
146+ return res . status ( 204 ) ;
147+ } ) ;
148+
116149export default router ;
0 commit comments