Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Replace flask-jwt with flask-jwt-extended #1

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 2 commits into from
Jul 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions project/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
from __future__ import absolute_import, print_function, unicode_literals

from flask import request, jsonify
from flask_jwt import jwt_required, current_identity

from flask_jwt_extended import (create_access_token,create_refresh_token,
get_jwt_identity,jwt_required,jwt_refresh_token_required)
from project.views import views_bp
from project.views.oauth import jwt, authenticate
from project.views.oauth import authenticate


class UserNotFoundException(Exception):
Expand Down Expand Up @@ -42,9 +42,13 @@ def login():
if not user:
raise UserNotFoundException("User not found!")

access_token = jwt.jwt_encode_callback(user)
access_token = create_access_token(identity=user.id,fresh=False)
refresh_token = create_refresh_token(user.id)

resp = jsonify({"access_token": str(access_token, "utf-8")})
resp = jsonify({
"access_token": str(access_token, "utf-8"),
"refresh_token": str(refresh_token,"utf-8")
})
resp.status_code = 200

# add token to response headers - so SwaggerUI can use it
Expand All @@ -71,7 +75,27 @@ def protected():
200:
description: User successfully accessed the content.
"""
resp = jsonify({"protected": "{}".format(current_identity)})
resp = jsonify({"protected": "{}".format(get_jwt_identity())})
resp.status_code = 200

return resp

@views_bp.route("/refresh-token",methods=["POST"])
@jwt_refresh_token_required
def refresh_token():
"""
Refresh Token Method
---
description: Refresh Access Tokens of the user
responses:
200:
description: User has generated new access tokens
"""
current_user = get_jwt_identity()
access_token = create_access_token(identity=current_user,fresh=False)
resp = jsonify({"access_token": str(access_token, "utf-8")})

resp.status_code = 200
resp.headers.extend({'jwt-token': access_token})
return resp

2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ flasgger==0.8.1
Flask==0.12.2
Flask-Bcrypt==0.7.1
Flask-Injector==0.10.1
Flask-JWT==0.3.2
Flask-JWT-Extended==3.24.1
Flask-Login==0.4.1
Flask-OpenTracing==0.1.8
Flask-Script==2.0.6
Expand Down