@@ -32,7 +32,18 @@ Follow the following steps and you're good to go! Important:
3232
3333``` typescript
3434...
35-
35+ export default {
36+ port: process .env .PORT || 3000 ,
37+ DB: {
38+ URI:
39+ process .env .MONGO_URI ||
40+ " mongodb://127.0.0.1:27017/restapijwttspassport_db" ,
41+ USER: process .env .MONGO_USER ,
42+ PASSWORD: process .env .MONGO_PASSWORD ,
43+ },
44+ secretKey:
45+ process .env .SECRET_KEY || " T_LHqi1hEFpsxPZ2heE.wkUKn3k3QSw.DdEK4EQ" ,
46+ };
3647...
3748```
3849
@@ -41,14 +52,41 @@ Follow the following steps and you're good to go! Important:
4152``` typescript
4253...
4354
55+ const router = Router ();
56+
57+ router .post (" /signin" , signIn );
58+ router .post (" signup" , signUp );
59+ router .get (
60+ " /profile" ,
61+ passport .authenticate (" jwt" , { session: false }),
62+ (req , res ) => {
63+ res .send (" Success!!" );
64+ }
65+ );
66+
4467...
4568```
4669
4770### Middlewares
4871
4972``` typescript
5073...
51-
74+ const opts: StrategyOptions = {
75+ jwtFromRequest: ExtractJwt .fromAuthHeaderAsBearerToken (),
76+ secretOrKey: config .secretKey ,
77+ };
78+
79+ export default new Strategy (opts , (payload , done ) => {
80+ try {
81+ const user = User .findById (payload .id );
82+ if (user ) {
83+ return done (null , user );
84+ }
85+ return done (null , false );
86+ } catch (error ) {
87+ console .log (error );
88+ }
89+ });
5290...
5391```
5492
@@ -57,7 +95,48 @@ Follow the following steps and you're good to go! Important:
5795
5896``` typescript
5997...
60-
98+ export const signUp = async (
99+ req : Request ,
100+ res : Response
101+ ): Promise <Response > => {
102+ if (! req .body .email || ! req .body .password ) {
103+ return res
104+ .status (400 )
105+ .json ({ msg: " Please, send your email and password." });
106+ }
107+
108+ const user = await User .findOne ({ email: req .body .email });
109+ if (user ) {
110+ return res .status (400 ).json ({ msg: " User already exixts!" });
111+ }
112+
113+ const newUser = new User (req .body );
114+ await newUser .save ();
115+ return res .status (201 ).json (newUser );
116+ };
117+
118+ export const signIn = async (
119+ req : Request ,
120+ res : Response
121+ ): Promise <Response > => {
122+ if (! req .body .email || ! req .body .password ) {
123+ return res
124+ .status (400 )
125+ .json ({ msg: " Please, send your email and password." });
126+ }
127+
128+ const user = await User .findOne ({ email: req .body .email });
129+ if (! user ) {
130+ return res .status (400 ).json ({ msg: " User does not exixts!" });
131+ }
132+
133+ const isMatch = await bcrypt .compare (req .body .password , user .password );
134+ if (isMatch ) {
135+ return res .status (201 ).json ({ token: createToken (user ) });
136+ }
137+
138+ return res .status (400 ).json ({ msg: " The email or password are incorrect!" });
139+ };
61140...
62141
63142```
@@ -66,7 +145,60 @@ Follow the following steps and you're good to go! Important:
66145
67146``` typescript
68147...
69-
148+ const UserSchema = new Schema (
149+ {
150+ displayName: {
151+ type: String ,
152+ required: true ,
153+ },
154+ username: {
155+ type: String ,
156+ minlength: 4 ,
157+ required: true ,
158+ },
159+ email: {
160+ type: String ,
161+ unique: true ,
162+ trim: true ,
163+ lowercase: true ,
164+ required: true ,
165+ },
166+ password: {
167+ type: String ,
168+ minlength: 6 ,
169+ required: true ,
170+ },
171+ avatar: {
172+ type: String ,
173+ maxlength: 512 ,
174+ required: false ,
175+ },
176+ role: {
177+ type: String ,
178+ enum: [" SUPERADMIN" , " ADMIN" , " USER" ],
179+ default: " USER" ,
180+ },
181+ status: {
182+ type: Boolean ,
183+ default: true ,
184+ },
185+ },
186+ {
187+ timestamps: true ,
188+ }
189+ );
190+
191+ UserSchema .pre <IUser >(" save" , async function (next ) {
192+ const user = this ;
193+ if (! user .isModified (" password" )) return next ();
194+
195+ const salt = await bcrypt .genSalt (10 );
196+ const hash = await bcrypt .hash (user .password , salt );
197+ user .password = hash ;
198+ next ();
199+ });
200+
201+ export default model <IUser >(" User" , UserSchema );
70202...
71203```
72204
0 commit comments