Skip to content
Snippets Groups Projects
Commit 8aff4451 authored by guoguo.yu's avatar guoguo.yu :speech_balloon:
Browse files

I thank GOD that this works kind of

parent b6f1c970
No related branches found
No related tags found
No related merge requests found
...@@ -60,10 +60,28 @@ func createSession(session Session) error { ...@@ -60,10 +60,28 @@ func createSession(session Session) error {
// Get a handle to the sessions collection // Get a handle to the sessions collection
sessionsCollection := client.Database("authentication").Collection("sessions") sessionsCollection := client.Database("authentication").Collection("sessions")
// delete all sessions for the user from the sessions collection
_, err = sessionsCollection.DeleteMany(context.Background(), bson.M{"username": session.Username})
// Insert the session into the sessions collection // Insert the session into the sessions collection
_, err = sessionsCollection.InsertOne(context.Background(), session) _, err = sessionsCollection.InsertOne(context.Background(), session)
return err return err
} }
func deleteUserSessions(username string) error {
// Connect to the MongoDB database
clientOptions := options.Client().ApplyURI(URI)
client, err := mongo.Connect(context.Background(), clientOptions)
if err != nil {
return err
}
defer client.Disconnect(context.Background())
// Delete all sessions for the user from the sessions collection
_, err = sessionsCollection.DeleteMany(context.Background(), bson.M{"username": username})
return err
}
func getSession(sessionID string) (Session, error) { func getSession(sessionID string) (Session, error) {
// Connect to the MongoDB database // Connect to the MongoDB database
clientOptions := options.Client().ApplyURI(URI) clientOptions := options.Client().ApplyURI(URI)
...@@ -1116,43 +1134,6 @@ func successSignin(w http.ResponseWriter, r *http.Request) { ...@@ -1116,43 +1134,6 @@ func successSignin(w http.ResponseWriter, r *http.Request) {
return return
} }
id := uuid.NewV4()
// Create a new session object
session := Session{
ID: id.String(),
Username: r.FormValue("username"),
CreationTime: time.Now(),
}
// Connect to the MongoDB database
clientOptions := options.Client().ApplyURI(URI)
client, err := mongo.Connect(context.Background(), clientOptions)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer client.Disconnect(context.Background())
// Store the session in the database
err = createSession(session)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
cookie := &http.Cookie{
Name: "session_id",
Value: session.ID,
Expires: time.Now().Add(24 * time.Hour),
HttpOnly: true,
}
http.SetCookie(w, cookie)
username := cookie.Value
fmt.Println("printing username from cookie value")
fmt.Println(username)
err = tmpl.Execute(w, data) err = tmpl.Execute(w, data)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
...@@ -1520,5 +1501,16 @@ func signInHandler(w http.ResponseWriter, r *http.Request) { ...@@ -1520,5 +1501,16 @@ func signInHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
cookie := &http.Cookie{
Name: "session_id",
Value: session.ID,
Expires: time.Now().Add(24 * time.Hour),
HttpOnly: true,
}
http.SetCookie(w, cookie)
fmt.Println("printing username from cookie value")
fmt.Println(username)
http.Redirect(w, r, "/profile", http.StatusTemporaryRedirect) http.Redirect(w, r, "/profile", http.StatusTemporaryRedirect)
} }
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment