1
+ const Team = require ( '../models/Team' ) ;
2
+ const bcrypt = require ( 'bcrypt' ) ;
3
+ const OBBS = require ( '../services/OBBS' ) ;
4
+ const generateToken = require ( '../services/generateToken' ) ;
5
+ const jwt = require ( 'jsonwebtoken' ) ;
6
+
7
+ function AuthController ( ) {
8
+ return {
9
+ async Me ( req , res ) {
10
+ const { token } = req . body ;
11
+ // console.log(token)
12
+ try {
13
+ const decoded = jwt . verify ( token , process . env . JWT_KEY || "@njkddm#jkim" ) ;
14
+ const teamId = decoded . id ;
15
+ const team = await Team . findById ( teamId ) ;
16
+
17
+ if ( ! team ) {
18
+ return res . status ( 500 ) . json ( {
19
+ team : null ,
20
+ } ) ;
21
+ } else {
22
+ return res . status ( 200 ) . json ( { team } ) ;
23
+ }
24
+ } catch ( err ) {
25
+ return res . status ( 500 ) . json ( {
26
+ team : null ,
27
+ message : "Token invalid!"
28
+ } ) ;
29
+ }
30
+ } ,
31
+ async Login ( req , res ) {
32
+ const { email, password } = req . body ;
33
+ if ( ! email || ! password ) {
34
+ return res . status ( 400 ) . json ( { message : 'Please fill all the details!' } ) ;
35
+ }
36
+ try {
37
+ const team_exist = await Team . findOne ( { email : email } ) ;
38
+ if ( ! team_exist ) {
39
+ return res . status ( 400 ) . json ( { message : 'Team Not Found, Please Register.' } ) ;
40
+ } else if ( team_exist . email === email ) {
41
+ const is_password_correct = await bcrypt . compare ( password , team_exist . password ) . then ( ( data ) => {
42
+ return data ;
43
+ } ) . catch ( err => {
44
+ console . log ( err )
45
+ return res . status ( 403 ) . json ( { message : 'Internal Server Error' } ) ;
46
+ } ) ;
47
+ if ( is_password_correct ) {
48
+ const token = generateToken ( team_exist . id ) ;
49
+ return res . status ( 200 ) . json ( { message : "Login success!" , teamData : OBBS ( team_exist ) , token : token } ) ;
50
+ } else if ( ! is_password_correct ) {
51
+ return res . status ( 403 ) . json ( { message : 'Invalid password' } ) ;
52
+ }
53
+
54
+ return res . status ( 500 ) . json ( { message : 'Internal Server Error' } ) ;
55
+ }
56
+ } catch ( error ) {
57
+ console . log ( 'team finding?' , error )
58
+ return res . status ( 500 ) . json ( { message : 'Internal Server Error' } )
59
+ }
60
+ } ,
61
+ async Register ( req , res ) {
62
+ const { email, password, teamname, homeUniversity, activemembers, attendeventmembers, teamrepresentetive, emailrepresentetive, numberrepresentetive, teamlogo, officialteamname, teamaddress, country, postalcode } = req . body ;
63
+
64
+ if ( ! email || ! password || ! teamname || ! homeUniversity || ! activemembers || ! attendeventmembers || ! teamrepresentetive || ! emailrepresentetive || ! numberrepresentetive || ! teamlogo || ! officialteamname || ! teamaddress || ! country || ! postalcode ) {
65
+ return res . status ( 400 ) . json ( { message : 'Please fill all the details!' } ) ;
66
+ }
67
+ try {
68
+ const team_exist = await Team . findOne ( { email : email } ) ;
69
+ if ( team_exist ) {
70
+ return res . status ( 400 ) . json ( { message : 'Team Already Exists' } ) ;
71
+ } else {
72
+ try {
73
+ const hashed_password = await bcrypt . hash ( password , 10 ) . then ( ( data ) => {
74
+ return data ;
75
+ } ) . catch ( err => {
76
+ throw new Error ( err ) ;
77
+ } ) ;
78
+ await Team . create ( { email, password : hashed_password , teamname, homeUniversity, activemembers, attendeventmembers, teamrepresentetive, emailrepresentetive, numberrepresentetive, teamlogo, officialteamname, teamaddress, country, postalcode } ) . then ( ( result ) => {
79
+ const token = generateToken ( result . id ) ;
80
+ return res . status ( 200 ) . json ( { message : 'Created!' , token : token } ) ;
81
+ } ) . catch ( error => {
82
+ console . log ( 'Couldnt create team' , error )
83
+ return res . status ( 500 ) . json ( { message : 'Internal Server Error' } )
84
+ } ) ;
85
+ } catch ( error ) {
86
+ return res . status ( 500 ) . json ( { message : 'Internal Server Error' } )
87
+ }
88
+ }
89
+ } catch ( error ) {
90
+ return res . status ( 500 ) . json ( { message : 'Internal Server Error' } )
91
+ }
92
+ }
93
+ }
94
+ }
95
+
96
+ module . exports = AuthController ;
0 commit comments