@@ -2,7 +2,7 @@ var express = require('express');
2
2
var app = express ( ) ;
3
3
var cors = require ( 'cors' ) ;
4
4
var dal = require ( './dal.js' ) ;
5
- const e = require ( 'express ' ) ;
5
+ const { generateToken , verifyToken } = require ( './authMid.js ' ) ;
6
6
7
7
// used to serve static files from public directory
8
8
app . use ( express . static ( 'public' ) ) ;
@@ -35,28 +35,27 @@ app.get('/account/create/:name/:email/:password', function (req, res) {
35
35
36
36
// login user
37
37
app . get ( '/account/login/:email/:password' , function ( req , res ) {
38
-
39
- dal . find ( req . params . email ) .
40
- then ( ( user ) => {
41
-
42
- // if user exists, check password
43
- if ( user . length > 0 ) {
44
- if ( user [ 0 ] . password === req . params . password ) {
45
- res . send ( user [ 0 ] ) ;
46
- }
47
- else {
48
- res . send ( 'Login failed: wrong password' ) ;
38
+ dal . find ( req . params . email )
39
+ . then ( ( user ) => {
40
+ if ( user . length > 0 ) {
41
+ if ( user [ 0 ] . password === req . params . password ) {
42
+ const token = generateToken ( { email : req . params . email } ) ;
43
+ res . json ( { user : user [ 0 ] , token } ) ; // Send user and token upon successful login
44
+ } else {
45
+ res . status ( 401 ) . send ( 'Login failed: wrong password' ) ;
49
46
}
47
+ } else {
48
+ res . status ( 404 ) . send ( 'Login failed: user not found' ) ;
50
49
}
51
- else {
52
- res . send ( 'Login failed: user not found' ) ;
53
- }
54
- } ) ;
55
-
50
+ } )
51
+ . catch ( ( error ) => {
52
+ console . error ( 'Error:' , error ) ;
53
+ res . status ( 500 ) . send ( 'An error occurred while logging in' ) ;
54
+ } ) ;
56
55
} ) ;
57
56
58
57
// find user account
59
- app . get ( '/account/find/:email' , function ( req , res ) {
58
+ app . get ( '/account/find/:email' , verifyToken , function ( req , res ) {
60
59
61
60
dal . find ( req . params . email ) .
62
61
then ( ( user ) => {
@@ -66,7 +65,7 @@ app.get('/account/find/:email', function (req, res) {
66
65
} ) ;
67
66
68
67
// find one user by email - alternative to find
69
- app . get ( '/account/findOne/:email' , function ( req , res ) {
68
+ app . get ( '/account/findOne/:email' , verifyToken , function ( req , res ) {
70
69
71
70
dal . findOne ( req . params . email ) .
72
71
then ( ( user ) => {
@@ -77,7 +76,7 @@ app.get('/account/findOne/:email', function (req, res) {
77
76
78
77
79
78
// update - deposit/withdraw amount
80
- app . get ( '/account/update/:email/:amount' , function ( req , res ) {
79
+ app . get ( '/account/update/:email/:amount' , verifyToken , function ( req , res ) {
81
80
82
81
var amount = Number ( req . params . amount ) ;
83
82
@@ -89,7 +88,7 @@ app.get('/account/update/:email/:amount', function (req, res) {
89
88
} ) ;
90
89
91
90
// all accounts
92
- app . get ( '/account/all' , function ( req , res ) {
91
+ app . get ( '/account/all' , verifyToken , function ( req , res ) {
93
92
94
93
dal . all ( ) .
95
94
then ( ( docs ) => {
0 commit comments