/
SignOut.go
42 lines (33 loc) · 1001 Bytes
/
SignOut.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"log" // Logs
"net" // Server logic
"net/http" // Server logic
"time" // Timing
// Database
"context"
"go.mongodb.org/mongo-driver/bson"
)
// SignOut handles request for signing out
func SignOut(w http.ResponseWriter, r *http.Request) {
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
log.Println("/sign_out: Cannot discover user's IP")
ErrorHandler(w, r, http.StatusInternalServerError)
return
}
userCredential := Credential{
UserIP: net.ParseIP(ip),
EnterTime: time.Now(),
EndTime: time.Now().Add(CredCur),
}
if userCredential.UserIP == nil {
log.Println("/sign_out: Cannot discover user's IP")
ErrorHandler(w, r, http.StatusInternalServerError)
return
}
credentialsCollection := client.Database("Judex").Collection("credentials")
filter := bson.D{{Key: "userip", Value: userCredential.UserIP}}
credentialsCollection.DeleteMany(context.TODO(), filter)
http.Redirect(w, r, "/", http.StatusSeeOther)
}