File tree 1 file changed +32
-0
lines changed
1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
1
+ const jwt = require ( 'jsonwebtoken' )
2
+ const { BadRequestError } = require ( '../errors' )
3
+
4
+ const login = async ( req , res ) => {
5
+ const { username, password } = req . body
6
+
7
+ if ( ! username || ! password ) {
8
+ throw new BadRequestError ( 'Please provide email and password' )
9
+ }
10
+
11
+ const id = new Date ( ) . getDate ( )
12
+
13
+ const token = jwt . sign ( { id, username } , process . env . JWT_SECRET , {
14
+ expiresIn : '30d' ,
15
+ } )
16
+
17
+ res . status ( 200 ) . json ( { msg : 'user created' , token } )
18
+ }
19
+
20
+ const dashboard = async ( req , res ) => {
21
+ const luckyNumber = Math . floor ( Math . random ( ) * 100 )
22
+
23
+ res . status ( 200 ) . json ( {
24
+ msg : `Hello, ${ req . user . username } ` ,
25
+ secret : `Here is your authorized data, your lucky number is ${ luckyNumber } ` ,
26
+ } )
27
+ }
28
+
29
+ module . exports = {
30
+ login,
31
+ dashboard,
32
+ }
You can’t perform that action at this time.
0 commit comments