Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
I
idatg2204-project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
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
Steffen Martinsen
idatg2204-project
Commits
788bd077
Commit
788bd077
authored
1 year ago
by
Gisli Nielsen
Browse files
Options
Downloads
Patches
Plain Diff
Changed to use userId in cookie
parent
2585fdea
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Backend/routes/login.py
+16
-8
16 additions, 8 deletions
Backend/routes/login.py
with
16 additions
and
8 deletions
Backend/routes/login.py
+
16
−
8
View file @
788bd077
...
...
@@ -16,26 +16,32 @@ def post_login():
# Check if user exists and/or password exists
cur
=
mysql
.
connection
.
cursor
()
cur
.
execute
(
'''
SELECT user.email, user.hash, user.salt FROM user WHERE user.email = %s
'''
,
(
email
,))
cur
.
execute
(
'''
SELECT
user.user_id,
user.email, user.hash, user.salt FROM user WHERE user.email = %s
'''
,
(
email
,))
dbData
=
cur
.
fetchall
()
cur
.
close
()
if
len
(
dbData
)
<=
0
:
return
jsonify
({
"
message
"
:
"
Incorrect login information.
"
}),
400
# Deconstruct dbData[0]
userId
,
email
,
hash
,
salt
=
dbData
[
0
]
# Check if user exists
if
len
(
dbData
)
<=
0
:
return
jsonify
({
"
message
"
:
"
Incorrect login information.
"
}),
400
salt
=
dbData
[
0
][
2
].
encode
(
"
utf-8
"
)
hash
=
hash_function
(
password
,
salt
)
# Retrieve the password and hash the password to check with DB hash
salt
=
salt
.
encode
(
"
utf-8
"
)
new_hash
=
hash_function
(
password
,
salt
)
# Password check
if
hash
.
decode
(
"
utf-8
"
)
!=
dbData
[
0
][
1
]
:
if
new_
hash
.
decode
(
"
utf-8
"
)
!=
hash
:
return
jsonify
({
"
message
"
:
"
Incorrect login information.
"
}),
400
# Set cookie
# Set cookie
and make it last 24 hours
response
=
make_response
(
jsonify
({
"
message
"
:
"
Login successful
"
}))
# Cookie lasts 24 hours
response
.
set_cookie
(
'
logged_in
'
,
email
,
max_age
=
60
*
60
*
24
)
userId
=
str
(
userId
)
response
.
set_cookie
(
'
logged_in
'
,
userId
,
max_age
=
60
*
60
*
24
)
return
response
,
200
def
post_register
():
...
...
@@ -61,6 +67,7 @@ def post_register():
if
len
(
data
)
>
0
:
return
jsonify
({
"
message
"
:
"
Email already exists
"
}),
400
# Instert the user information into the DB
cur
=
mysql
.
connection
.
cursor
()
cur
.
execute
(
'''
INSERT INTO user_details (email, first_name, last_name, address) VALUES (%s, %s, %s, %s)
'''
,
(
email
,
firstname
,
lastname
,
address
))
cur
.
execute
(
'''
INSERT INTO user (email, hash, salt) VALUES (%s, %s, %s)
'''
,
(
email
,
password
,
salt
))
...
...
@@ -72,6 +79,7 @@ def post_register():
def
hash_function
(
password
,
salt
):
password
=
password
.
encode
(
"
utf-8
"
)
# Hash the password and return the hashed value
hashed
=
bcrypt
.
hashpw
(
password
,
salt
)
return
hashed
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