1
1
import md5 from "md5" ;
2
- import { users , message } from "./controllers/userControllers" ;
3
2
import jwt from "jsonwebtoken" ;
3
+ import { isUser , userExists } from "./database/userQueries" ;
4
+ import { unauthorized } from "./errors/unauthorized" ;
5
+ import { badRequest } from "./errors/badRequest" ;
4
6
5
- export const verifyLogin = async function ( ctx , next ) {
7
+ export const verifyUser = ( ctx , next ) => {
8
+ if ( ! ctx . request . headers . authorization ) {
9
+ unauthorized ( ctx , "Unauthorized request" ) ;
10
+ return ;
11
+ }
12
+ const token = ctx . request . headers . authorization . split ( " " ) [ 1 ] ;
13
+ if ( ! token ) {
14
+ unauthorized ( ctx , "Access denied. No token provided." ) ;
15
+ return ;
16
+ }
17
+ try {
18
+ const decoded = jwt . verify ( token , process . env . JWT_SECRET ) ;
19
+ ctx . request . user = decoded . user ;
20
+ return next ( ) ;
21
+ } catch ( err ) {
22
+ unauthorized ( ctx , "Invalid token" ) ;
23
+ return ;
24
+ }
25
+ } ;
26
+
27
+ export const verifyLogin = async ( ctx , next ) => {
6
28
const user = ctx . request . body ;
7
29
if ( ! user . username || ! user . email || ! user . password ) {
8
- ctx . status = 400 ;
9
- ctx . body = message (
10
- false ,
11
- "Provide username, email, and password to login!"
12
- ) ;
30
+ badRequest ( ctx , "Provide username, email, and password to login!" ) ;
13
31
return ;
14
32
}
15
33
const { username, email, password } = user ;
16
- const foundUser = users . find ( ( user ) => {
17
- return user . email === email && user . username === username ;
18
- } ) ;
34
+ const foundUser = await isUser ( email , username ) ;
19
35
if ( ! foundUser ) {
20
- ctx . status = 400 ;
21
- ctx . body = message ( false , "Invalid username, email or password!" ) ;
36
+ badRequest ( ctx , "Invalid username, email or password!" ) ;
22
37
return ;
23
38
}
24
- const isPasswordValid = ( await md5 ( password ) ) === foundUser . password ;
39
+ const isPasswordValid = md5 ( password ) === foundUser . password ;
25
40
if ( ! isPasswordValid ) {
26
- ctx . status = 400 ;
27
- ctx . body = message ( false , "Invalid username, email or password!" ) ;
41
+ badRequest ( ctx , "Invalid username, email or password!" ) ;
28
42
return ;
29
43
}
30
- await next ( ) ;
44
+ return next ( ) ;
31
45
} ;
32
46
33
- export const verifyUserDuplicates = function ( ctx , next ) {
47
+ export const verifyUserDuplicates = async ( ctx , next ) => {
34
48
const { email, password, username } = ctx . request . body ;
35
49
if ( ! email || ! password ) {
36
- ctx . status = 400 ;
37
- ctx . body = { success : false , message : "Email and password are required." } ;
50
+ badRequest ( ctx , "Email and password are required." ) ;
38
51
return ;
39
52
}
40
53
if ( ! username ) {
41
- ctx . status = 400 ;
42
- ctx . body = { success : false , message : "Username is required." } ;
54
+ badRequest ( ctx , "Username is required." ) ;
43
55
return ;
44
56
}
45
57
if (
46
- users . find ( ( ele ) => {
47
- return ele . email === email || ele . username === username ;
48
- } )
58
+ ( await userExists ( email , username ) ) ||
59
+ badRequest ( ctx , "Seomething went wrong. Try again after some time." )
49
60
) {
50
- ctx . status = 400 ;
51
- ctx . body = message (
52
- false ,
61
+ badRequest (
62
+ ctx ,
53
63
"User with these credentials already exists. Provide a different username or email!"
54
64
) ;
55
65
return ;
56
66
}
57
- next ( ) ;
67
+ return next ( ) ;
58
68
} ;
59
69
60
70
export const verifyUserRegex = ( ctx , next ) => {
@@ -63,45 +73,18 @@ export const verifyUserRegex = (ctx, next) => {
63
73
/ ^ [ a - z A - Z 0 - 9 . ! # $ % & ' * + / = ? ^ _ ` { | } ~ - ] + @ [ a - z A - Z 0 - 9 ] (?: [ a - z A - Z 0 - 9 - ] { 0 , 61 } [ a - z A - Z 0 - 9 ] ) ? (?: \. [ a - z A - Z 0 - 9 ] (?: [ a - z A - Z 0 - 9 - ] { 0 , 61 } [ a - z A - Z 0 - 9 ] ) ? ) * $ / ;
64
74
const usernameRegex = / ^ [ a - z A - Z 0 - 9 . _ ] { 4 , 15 } $ / ;
65
75
if ( ! usernameRegex . test ( username ) ) {
66
- ctx . status = 400 ;
67
- ctx . body = {
68
- success : false ,
69
- message :
70
- "Username must be 4 to 15 characters long and should not contain any special characters!" ,
71
- } ;
76
+ badRequest (
77
+ ctx ,
78
+ "Username must be 4 to 15 characters long and should not contain any special characters!"
79
+ ) ;
72
80
return ;
73
81
}
74
82
if ( ! emailRegex . test ( email ) || password . length < 8 || password . length > 15 ) {
75
- ctx . status = 400 ;
76
- ctx . body = {
77
- success : false ,
78
- message :
79
- "Email or password is in incorrect format. Check email format and password must be at least 8 characters long." ,
80
- } ;
81
- return ;
82
- }
83
- next ( ) ;
84
- } ;
85
-
86
- export const verifyUser = function ( ctx , next ) {
87
- if ( ! ctx . request . headers . authorization ) {
88
- ctx . status = 401 ;
89
- ctx . body = "Unauthorized request!" ;
90
- return ;
91
- }
92
- const token = ctx . request . headers . authorization . split ( " " ) [ 1 ] ;
93
- if ( ! token ) {
94
- ctx . status = 401 ;
95
- ctx . body = "Access denied. No token provided." ;
96
- return ;
97
- }
98
- try {
99
- const decoded = jwt . verify ( token , process . env . JWT_SECRET ) ;
100
- ctx . request . user = decoded . user ;
101
- next ( ) ;
102
- } catch ( err ) {
103
- ctx . status = 400 ;
104
- ctx . body = "Invalid token" ;
83
+ badRequest (
84
+ ctx ,
85
+ "Email or password is in incorrect format. Check email format and password must be at least 8 characters long."
86
+ ) ;
105
87
return ;
106
88
}
89
+ return next ( ) ;
107
90
} ;
0 commit comments