-
Notifications
You must be signed in to change notification settings - Fork 1
Feature/flask smorest auth #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| from email_validator import EmailNotValidError | ||
| from flask import jsonify, make_response | ||
| from flask.views import MethodView | ||
| from flask_jwt_extended import ( | ||
| create_access_token, | ||
| create_refresh_token, | ||
| get_jwt_identity, | ||
| jwt_required, | ||
| ) | ||
| from flask_smorest import Blueprint, abort | ||
| from sqlalchemy.exc import IntegrityError | ||
|
|
||
| from app import db | ||
| from app.models import User | ||
| from app.schemas import AuthIn, AuthOut | ||
|
|
||
| bp = Blueprint("auth", __name__) | ||
|
|
||
|
|
||
| @bp.route("/register") | ||
| class Register(MethodView): | ||
| @bp.arguments(AuthIn) | ||
| @bp.response(201) | ||
| def post(self, data): | ||
| """ | ||
| Register a new user. | ||
| --- | ||
| tags: | ||
| - User | ||
| description: Register a new user. | ||
| requestBody: | ||
| required: true | ||
| description: email - Email id <br> password - Password | ||
| content: | ||
| application/json: | ||
| schema: | ||
| type: object | ||
| required: | ||
| - password | ||
| properties: | ||
| email: | ||
| type: string | ||
| password: | ||
| type: string | ||
| responses: | ||
| 201: | ||
| description: User registered successfully. | ||
| 400: | ||
| description: Invalid input. | ||
| 409: | ||
| description: Email already exists. | ||
| 500: | ||
| description: Internal Server Error. | ||
| """ | ||
|
|
||
| user = User() | ||
| user.set_password(data["password"]) | ||
|
|
||
| try: | ||
| user.set_email(data["email"]) | ||
piyush-jaiswal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| db.session.add(user) | ||
| db.session.commit() | ||
| except IntegrityError: | ||
| db.session.rollback() | ||
| abort(make_response(jsonify(error="Email already exists"), 409)) | ||
| except EmailNotValidError as e: | ||
| abort( | ||
| make_response(jsonify(code="invalid_email_format", error=str(e)), 422) | ||
| ) | ||
piyush-jaiswal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return {"message": "Registered!"} | ||
|
|
||
|
|
||
| @bp.route("/login") | ||
| class Login(MethodView): | ||
| """Login a user and return access & refresh tokens.""" | ||
|
|
||
| @bp.arguments(AuthIn) | ||
| @bp.response(200, AuthOut) | ||
| def post(self, data): | ||
| """ | ||
| Login a user. | ||
| --- | ||
| tags: | ||
| - User | ||
| description: Login a user. | ||
| requestBody: | ||
| required: true | ||
| description: email - Email id <br> password - Password | ||
| content: | ||
| application/json: | ||
| schema: | ||
| type: object | ||
| required: | ||
| - password | ||
| properties: | ||
| email: | ||
| type: string | ||
| password: | ||
| type: string | ||
| responses: | ||
| 200: | ||
| description: User logged in successfully. | ||
| 400: | ||
| description: Invalid input. | ||
| 401: | ||
| description: Invalid email or password. | ||
| 500: | ||
| description: Internal Server Error. | ||
| """ | ||
| user = User.get(email=data["email"]) | ||
| if not user or not user.check_password(data["password"]): | ||
| return abort( | ||
| make_response( | ||
| jsonify( | ||
| error="Invalid email or password. Check again or register." | ||
| ), | ||
| 401, | ||
| ) | ||
| ) | ||
piyush-jaiswal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return { | ||
| "access_token": create_access_token(identity=str(user.id), fresh=True), | ||
| "refresh_token": create_refresh_token(identity=str(user.id)), | ||
| } | ||
|
|
||
|
|
||
| @bp.route("/refresh") | ||
| class Refresh(MethodView): | ||
| """Get new access token using your refresh token.""" | ||
|
|
||
| @jwt_required(refresh=True) | ||
| @bp.response(200, AuthOut(partial=("access_token",))) | ||
| def post(self): | ||
| """ | ||
| Get new access token using your refresh token | ||
| --- | ||
| tags: | ||
| - User | ||
| description: Get new access token using your refresh token. | ||
| security: | ||
| - refresh_token: [] | ||
| responses: | ||
| 200: | ||
| description: New access token. | ||
| 401: | ||
| description: Token expired, missing or invalid. | ||
| 500: | ||
| description: Internal Server Error. | ||
| """ | ||
| identity = get_jwt_identity() | ||
| return {"access_token": create_access_token(identity=identity, fresh=False)} | ||
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.