@@ -10,29 +10,52 @@ import axios from "axios";
10
10
dotenv . config ( { path : "../../" } ) ;
11
11
12
12
13
- export default async function handler ( req : NextApiRequest , res : NextApiResponse ) {
14
- if ( req . method !== "POST" )
15
- return res . status ( 405 ) . json ( { message : "Method not allowed" } ) ;
13
+ export default async function handler ( req : NextApiRequest , res : NextApiResponse ) {
14
+ if ( req . method !== "POST" )
15
+ return res . status ( 405 ) . json ( {
16
+ message : "Method not allowed"
17
+ } ) ;
16
18
17
19
try {
18
- const data = { ...req . body } ;
20
+ const data = {
21
+ ...req . body
22
+ } ;
19
23
const xTrack = getXTrack ( req ) ;
20
- if ( ! xTrack ) return res . status ( 400 ) . json ( { success : false , message : "Invalid Request" } ) ;
24
+ if ( ! xTrack ) return res . status ( 400 ) . json ( {
25
+ success : false ,
26
+ message : "Invalid Request"
27
+ } ) ;
21
28
22
29
let tokenExpiry : string = "30d" ;
23
30
if ( ! data . username || ! data . password ) {
24
- return res . status ( 400 ) . json ( { success : false , message : "Missing username or password" } ) ;
31
+ return res . status ( 400 ) . json ( {
32
+ success : false ,
33
+ message : "Missing username or password"
34
+ } ) ;
25
35
}
26
-
27
- if ( ! data ) return res . status ( 400 ) . json ( { message : "Please provide all fields" } ) ;
28
36
29
- const account = await prisma . accounts . findFirst ( { where : { username : data . username . toLowerCase ( ) } } ) ;
30
- if ( ! account ) return res . status ( 400 ) . json ( { message : "Account not found" } ) ;
37
+ if ( ! data ) return res . status ( 400 ) . json ( {
38
+ message : "Please provide all fields"
39
+ } ) ;
40
+
41
+ const account = await prisma . accounts . findFirst ( {
42
+ where : {
43
+ username : data . username . toLowerCase ( )
44
+ }
45
+ } ) ;
46
+ if ( ! account ) return res . status ( 400 ) . json ( {
47
+ process,
48
+ message : "Account not found"
49
+ } ) ;
31
50
32
51
const isValid = await bcrypt . compare ( data . password , account . password ) ;
33
- if ( ! isValid ) return res . status ( 400 ) . json ( { message : "Some Credentials are incorrect" } ) ;
52
+ if ( ! isValid ) return res . status ( 400 ) . json ( {
53
+ message : "Some Credentials are incorrect"
54
+ } ) ;
34
55
35
- if ( ( account . twoFactor !== 0 && account . googleAuthCode ) && ! data . totp ) return res . status ( 400 ) . json ( { message : "2FA Code Required" } ) ;
56
+ if ( ( account . twoFactor !== 0 && account . googleAuthCode ) && ! data . totp ) return res . status ( 400 ) . json ( {
57
+ message : "2FA Code Required"
58
+ } ) ;
36
59
37
60
if ( account . twoFactor !== 0 && account . googleAuthCode ) {
38
61
const totpVerify = speakeasy . totp . verify ( {
@@ -41,16 +64,30 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
41
64
token : data . totp
42
65
} ) ;
43
66
44
- if ( ! totpVerify ) return res . status ( 400 ) . json ( { message : "Invalid 2FA Code" } ) ;
67
+ if ( ! totpVerify ) return res . status ( 400 ) . json ( {
68
+ message : "Invalid 2FA Code"
69
+ } ) ;
45
70
46
71
tokenExpiry = "90d" ;
47
72
}
48
73
49
- if ( account . banned ) return res . status ( 400 ) . json ( { message :
"Account is Banned. Contact: [email protected] " } ) ;
74
+ if ( account . banned ) return res . status ( 400 ) . json ( {
75
+ message :
"Account is Banned. Contact: [email protected] "
76
+ } ) ;
50
77
51
- const token = sign ( { id : account . id , time : Date . now ( ) } , `${ process . env . JWT_SECRET } ` , { expiresIn : tokenExpiry } ) ;
78
+ const token = sign ( {
79
+ id : account . id ,
80
+ time : Date . now ( )
81
+ } , `${ process . env . JWT_SECRET } ` , {
82
+ expiresIn : tokenExpiry
83
+ } ) ;
52
84
53
- await prisma . sessions . deleteMany ( { where : { accountId : account . id , token : token } } ) ;
85
+ await prisma . sessions . deleteMany ( {
86
+ where : {
87
+ accountId : account . id ,
88
+ token : token
89
+ }
90
+ } ) ;
54
91
55
92
await prisma . sessions . create ( {
56
93
data : {
@@ -87,8 +124,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
87
124
} ,
88
125
subject : "New Login Detected" ,
89
126
text : `Hello ${ account . username } ,\n\nA new login was detected from ${ res . data . city ?? "Unknown City" } , ${ res . data . region ?? "Unknown Region" } , ${ res . data . country ?? "Unknown Country" } .\n\nIf this was not you, please change your password immediately.\n\nRegards,\nRestoreCord` ,
90
- html :
91
- `
127
+ html : `
92
128
<!DOCTYPE html>
93
129
<html>
94
130
<head>
@@ -136,11 +172,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
136
172
success : true ,
137
173
message : "Login successful" ,
138
174
token : token ,
175
+ process
139
176
} ) ;
140
- }
141
- catch ( err : any ) {
177
+ } catch ( err : any ) {
142
178
console . error ( err ) ;
143
- if ( err ?. name === "ValidationError" ) return res . status ( 400 ) . json ( { success : false , message : err . errors [ 0 ] } ) ;
179
+ if ( err ?. name === "ValidationError" ) return res . status ( 400 ) . json ( {
180
+ success : false ,
181
+ message : err . errors [ 0 ]
182
+ } ) ;
144
183
return res . status ( 500 ) ;
145
- }
184
+ }
146
185
}
0 commit comments