Skip to content

Commit 37cf30c

Browse files
committed
Changed string of zID to be a variable
1 parent 997e2cf commit 37cf30c

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

server/server.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ func login(collection *mongo.Collection) echo.HandlerFunc {
150150
// Attempt to sign in using credentials
151151
zid := c.FormValue("zid")
152152
hashedZID := sha256.Sum256([]byte(zid))
153+
stringZID := string(hashedZID[:])
153154
username := zid + "ad.unsw.edu.au"
154155
password := c.FormValue("password")
155156

@@ -189,15 +190,16 @@ func login(collection *mongo.Collection) echo.HandlerFunc {
189190
tokenJWT := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
190191
tokenString, _ := tokenJWT.SignedString(jwtKey)
191192

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
193195
user := User{
194-
userID: string(hashedZID[:]),
196+
userID: stringZID,
195197
userToken: tokenString,
196198
role: "user", // Change this???
197199
}
198200

199201
var isValidUser *User
200-
userFilter := bson.D{{"userID", string(hashedZID[:])}}
202+
userFilter := bson.D{{"userID", stringZID}}
201203
err = collection.FindOne(context.TODO(), userFilter).Decode(&isValidUser)
202204

203205
if isValidUser == nil { // Never logged in before
@@ -213,7 +215,7 @@ func login(collection *mongo.Collection) echo.HandlerFunc {
213215
decodedTokenString, _ := decodedToken.SignedString(jwtKey)
214216

215217
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}}
217219
update := bson.D{
218220
{"$set", bson.D{
219221
{"userToken", decodedTokenString},

0 commit comments

Comments
 (0)