Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
Basic Auth Golang
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
guoguo.yu
Basic Auth Golang
Commits
8aff4451
Commit
8aff4451
authored
1 year ago
by
guoguo.yu
Browse files
Options
Downloads
Patches
Plain Diff
I thank GOD that this works kind of
parent
b6f1c970
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
main.go
+29
-37
29 additions, 37 deletions
main.go
output
+0
-0
0 additions, 0 deletions
output
with
29 additions
and
37 deletions
main.go
+
29
−
37
View file @
8aff4451
...
@@ -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
)
}
}
This diff is collapsed.
Click to expand it.
output
+
0
−
0
View file @
8aff4451
No preview for this file type
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment