Add login function to flask web app

Asked 1 years ago, Updated 1 years ago, 130 views

I don't have any examples right nowㅜㅜ I'm trying to write a web with flask Do not use the login module provided by flask-login or other flasks I have to create a login function by writing it from the beginning, and log in with a session instead of a cookie, and link it with db Can you make an easy example?

python-2.7 flask web login logout

2022-09-22 18:42

1 Answers

I used the session function on the flask to implement the login.

Although it is sloppy, it is divided into session[id]=True/False so that the login function is maintained only when it is true, but it is the logic below. I hope it helps you.

from flask import session, request, jsonify
from db import Login

#Create User
@flask.route('/users', methods=['POST'])
def UserCreate():
   id = as request id
   pw = pw with request

   #Save users in the DB.
   db_session.add(Login(Id=id, Password=pw))
   db_session.commit()
   return jsonify(Ture)

#Login
@flask.route('/users/login', methods=['POST'])
def UserLogin():
   id = as request id
   pw = pw with request

   #Compare DB data with id,pw
   #Create session if correct
   If Division Matching:
   session[id]=True #False at logout
   return jsonify(True)

   else:
   return jsonify(False)

#Logout
@flask.route('/users/logout', methods=['POST'])
def UserOut():
   id = logout id transferred to request

   session[id]=False
   return jsonify(True)

#Confirming
@flask.route('/user/restore', methods=['POST'])
def UserCheck():
   id = an active id that has been transferred to a request

   Limit if #session is true and still false.



2022-09-22 18:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.