@@ -2,9 +2,9 @@ import path from "path";
2
2
import { fileURLToPath } from "url" ;
3
3
import db from "../models/pgsql.js" ;
4
4
import bcrypt from "bcrypt" ;
5
- import nodemailer from ' nodemailer' ;
6
- import env from ' dotenv' ;
7
- import jwt from ' jsonwebtoken' ;
5
+ import nodemailer from " nodemailer" ;
6
+ import env from " dotenv" ;
7
+ import jwt from " jsonwebtoken" ;
8
8
9
9
env . config ( ) ;
10
10
@@ -13,19 +13,37 @@ const __dirname = path.dirname(__filename);
13
13
const views_path = path . join ( __dirname , ".." , "views" ) ;
14
14
15
15
var transporter = nodemailer . createTransport ( {
16
- service : ' gmail' ,
17
- host : ' smtp.gmail.com' ,
16
+ service : " gmail" ,
17
+ host : " smtp.gmail.com" ,
18
18
port : 587 ,
19
19
secure : false , // true for 465, false for other ports
20
20
auth : {
21
21
user : process . env . MAIL_ADDRESS ,
22
- pass : process . env . MAIL_PASSWORD
23
- }
22
+ pass : process . env . MAIL_PASSWORD ,
23
+ } ,
24
24
} ) ;
25
25
26
- async function verifyMail ( email , token ) {
26
+ function generateAcessToken ( id ) {
27
+ const token = jwt . sign ( { id : id } , process . env . ACESS_TOKEN_SECRET , {
28
+ expiresIn : "1h" ,
29
+ } ) ;
30
+ return token ;
31
+ }
32
+
33
+ function generateRefreshToken ( email , name ) {
34
+ const refreshToken = jwt . sign (
35
+ { "email" : email ,
36
+ "name" : name
37
+ } ,
38
+ process . env . REFRESH_TOKEN_SECRET ,
39
+ { expiresIn : "15d" }
40
+ ) ;
41
+ return refreshToken ;
42
+ }
43
+
44
+ async function verifyMail ( email , token ) {
27
45
// send mail with defined above transport object
28
- const link = ' http://localhost:3000/auth/token/' + token ;
46
+ const link = " http://localhost:3000/auth/token/" + token ;
29
47
const info = await transporter . sendMail ( {
30
48
from :
'"AO3" <[email protected] >' , // sender address
31
49
to : email , // user email address
@@ -56,12 +74,23 @@ export const post_login = (req, res) => {
56
74
res . status ( 401 ) . send ( "Invalid credentials" ) ;
57
75
return ;
58
76
}
77
+ if ( result . rows [ 0 ] . verified == 'false' ) {
78
+ res . status ( 403 ) . send ( "Please verify your mail address" ) ;
79
+ verifyMail ( email ,
80
+ jwt . sign ( { "email" : email } , process . env . ACESS_TOKEN_SECRET , {
81
+ expiresIn : "1h" ,
82
+ } ) ) ;
83
+ return ;
84
+ }
59
85
//auth sucess
60
86
//add jwt token logic here
61
- res . send ( "authSuccess" ) ;
62
- res . redirect ( "/dashboard" ) ;
87
+ const token = generateAcessToken ( result . rows [ 0 ] . id ) ;
88
+ const refreshToken = generateRefreshToken ( email , result . rows [ 0 ] . username ) ;
89
+ res . cookie ( "refreshToken" , refreshToken , { httpOnly : true } ) ;
90
+ res . cookie ( "token" , token , { httpOnly : true } ) ;
91
+ res . send ( "sucessful authentication" ) ;
92
+ // res.redirect("/dashboard");
63
93
} ) ;
64
- db . end ( ) ;
65
94
} ;
66
95
67
96
export const post_register = ( req , res ) => {
@@ -73,14 +102,13 @@ export const post_register = (req, res) => {
73
102
if ( err ) {
74
103
console . error ( err ) ;
75
104
res . status ( 500 ) . send ( "Server error" ) ;
76
- return ;
77
- }
78
- else if ( result . rowCount !== 0 ) {
105
+ return ;
106
+ } else if ( result . rowCount !== 0 ) {
79
107
res . status ( 409 ) . send ( "Email already exists" ) ;
80
108
return ;
81
109
}
82
110
// if user dosen't exist
83
- else {
111
+ else {
84
112
db . query (
85
113
"INSERT INTO login (username,email,password) VALUES($1,$2,$3)" ,
86
114
[ username , email , password ] ,
@@ -92,25 +120,30 @@ export const post_register = (req, res) => {
92
120
}
93
121
res . send ( "Registration successful" ) ;
94
122
// Node mailer will send mail to the user
95
- verifyMail ( email , jwt . sign ( { 'email' :email } , process . env . ACESS_TOKEN_SECRET , { expiresIn :'1h' } ) ) ;
123
+ verifyMail (
124
+ email ,
125
+ jwt . sign ( { "email" : email } , process . env . ACESS_TOKEN_SECRET , {
126
+ expiresIn : "1h" ,
127
+ } )
128
+ ) ;
96
129
}
97
- ) ;
130
+ ) ;
98
131
}
99
132
} ) ;
100
- db . end ( ) ;
101
- }
133
+ } ;
102
134
103
- export const get_token = ( req , res ) => {
135
+ export const get_token = ( req , res ) => {
104
136
const token = req . params . token ;
105
137
jwt . verify ( token , process . env . ACESS_TOKEN_SECRET , ( err , decoded ) => {
106
138
if ( err ) {
107
139
res . status ( 403 ) . send ( "Token is not valid" ) ;
108
140
return ;
141
+ } else {
142
+ db . query ( "UPDATE login SET verified = true WHERE email = $1;" , [
143
+ decoded . email ,
144
+ ] ) ;
145
+ res . redirect ( "/auth/login" ) ;
146
+ //add refresh token logic
109
147
}
110
- res . send ( "Token is valid" ) ;
111
- db . query ( "UPDATE login SET verified = true WHERE email = $1;" , [ decoded . email ] )
112
- // res.redirect("/login");
113
- //add refresh token logic
114
148
} ) ;
115
149
} ;
116
-
0 commit comments