-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
105 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,78 @@ | ||
''' | ||
Contains the initial api routes | ||
''' | ||
# from flask import request, jsonify | ||
from pollination import app # , bcrypt, db | ||
from pollination import app, bcrypt, login_manager, db | ||
from pollination.models import User, File, Species | ||
from flask import request, jsonify | ||
from flask_login import login_user, current_user, logout_user | ||
from flask_login import login_required, fresh_login_required | ||
|
||
|
||
@login_manager.unauthorized_handler | ||
def unauthorized(): | ||
''' | ||
Unauthorized | ||
''' | ||
return jsonify({"Invalid": "Unauthorized"}), 401 | ||
|
||
|
||
@app.route('/api/login', methods=['POST']) | ||
def login(): | ||
''' | ||
User Login and sets a cookie to be authenticated in the future | ||
''' | ||
data = request.json | ||
|
||
if (data is None): | ||
return jsonify({"Invalid": "Incorrect username or password"}), 401 | ||
if (data.get('username') is None): | ||
return jsonify({"Invalid": "Incorrect username or password"}), 401 | ||
if (data.get('password') is None): | ||
return jsonify({"Invalid": "Incorrect username or password"}), 401 | ||
|
||
user = db.session.scalars(db.select(User) | ||
.filter_by(username=data['username'])).one() | ||
|
||
if (user is None or user.password is None): | ||
# save value as a cookie | ||
return jsonify({"Invalid": "Incorrect username or password"}), 401 | ||
# return "Invalid" | ||
|
||
pass_check = bcrypt.check_password_hash(user.password, data['password']) | ||
if (pass_check is False): | ||
return jsonify({"Invalid": "Incorrect username or password"}), 401 | ||
# return "Invalid" | ||
|
||
# Check remember me token if the user wants a cookie | ||
login_user(user, remember=True) | ||
# user.verified = True | ||
return jsonify({"Success": "User is authenticated"}), 200 | ||
|
||
|
||
@app.route('/api/verify', methods=['GET']) | ||
@login_required | ||
def verify(): | ||
''' | ||
Verifies the User | ||
''' | ||
return jsonify({"Success": "User is authenticated"}), 200 | ||
|
||
|
||
@app.route('/api/change-password', methods=['POST']) | ||
@fresh_login_required | ||
def change_password(): | ||
''' | ||
Change password of the User | ||
ADD BELOW | ||
''' | ||
return jsonify({"Success": "User password changed"}), 200 | ||
|
||
|
||
@app.route('/api/test', methods=['GET', 'POST']) | ||
@login_required | ||
def test(): | ||
''' | ||
test | ||
''' | ||
return "This works" | ||
user: User = current_user | ||
return user.username |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
# pylint: skip-file | ||
from pollination import db | ||
from pollination import db, bcrypt | ||
from pollination.models import User | ||
|
||
if __name__ == "__main__": | ||
db.drop_all() | ||
db.create_all() | ||
|
||
user = User(username="moo", email="testing@gmail.com") | ||
hash = bcrypt.generate_password_hash("moo").decode('utf-8') | ||
user = User(username="moo", password=hash) | ||
db.session.add(user) | ||
db.session.commit() |