@@ -150,6 +150,7 @@ func login(collection *mongo.Collection) echo.HandlerFunc {
150
150
// Attempt to sign in using credentials
151
151
zid := c .FormValue ("zid" )
152
152
hashedZID := sha256 .Sum256 ([]byte (zid ))
153
+ stringZID := string (hashedZID [:])
153
154
username := zid + "ad.unsw.edu.au"
154
155
password := c .FormValue ("password" )
155
156
@@ -189,15 +190,16 @@ func login(collection *mongo.Collection) echo.HandlerFunc {
189
190
tokenJWT := jwt .NewWithClaims (jwt .SigningMethodHS256 , claims )
190
191
tokenString , _ := tokenJWT .SignedString (jwtKey )
191
192
192
- // Insert a new user into the collection if the token has expired or has never logged in before
193
+ // Insert a new user into the collection if user has never logged in before
194
+ // Or update the existing token if it has expired
193
195
user := User {
194
- userID : string ( hashedZID [:]) ,
196
+ userID : stringZID ,
195
197
userToken : tokenString ,
196
198
role : "user" , // Change this???
197
199
}
198
200
199
201
var isValidUser * User
200
- userFilter := bson.D {{"userID" , string ( hashedZID [:]) }}
202
+ userFilter := bson.D {{"userID" , stringZID }}
201
203
err = collection .FindOne (context .TODO (), userFilter ).Decode (& isValidUser )
202
204
203
205
if isValidUser == nil { // Never logged in before
@@ -213,7 +215,7 @@ func login(collection *mongo.Collection) echo.HandlerFunc {
213
215
decodedTokenString , _ := decodedToken .SignedString (jwtKey )
214
216
215
217
if ! decodedToken .Valid { // Logged in before but token is invalid - replace with new token
216
- filter := bson.D {{"userID" , string ( hashedZID [:]) }}
218
+ filter := bson.D {{"userID" , stringZID }}
217
219
update := bson.D {
218
220
{"$set" , bson.D {
219
221
{"userToken" , decodedTokenString },
0 commit comments