├── README.md ├── app.py ├── dashboard.png ├── instance └── database.db ├── login.png ├── register.png ├── requirements.txt └── templates ├── dashboard.html ├── index.html ├── login.html └── register.html /README.md: -------------------------------------------------------------------------------- 1 | # flask-authentication-system 2 | 3 | 4 | # Login page 5 | ![alt text](https://github.com/kritimyantra/flask-authentication-system/blob/main/login.png?raw=true) 6 | 7 | 8 | # Register page 9 | ![alt text](https://github.com/kritimyantra/flask-authentication-system/blob/main/register.png?raw=true) 10 | 11 | 12 | # Dashboard page 13 | ![alt text](https://github.com/kritimyantra/flask-authentication-system/blob/main/dashboard.png?raw=true) 14 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, request,render_template, redirect,session 2 | from flask_sqlalchemy import SQLAlchemy 3 | import bcrypt 4 | 5 | app = Flask(__name__) 6 | app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db' 7 | db = SQLAlchemy(app) 8 | app.secret_key = 'secret_key' 9 | 10 | class User(db.Model): 11 | id = db.Column(db.Integer, primary_key=True) 12 | name = db.Column(db.String(100), nullable=False) 13 | email = db.Column(db.String(100), unique=True) 14 | password = db.Column(db.String(100)) 15 | 16 | def __init__(self,email,password,name): 17 | self.name = name 18 | self.email = email 19 | self.password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8') 20 | 21 | def check_password(self,password): 22 | return bcrypt.checkpw(password.encode('utf-8'),self.password.encode('utf-8')) 23 | 24 | with app.app_context(): 25 | db.create_all() 26 | 27 | 28 | @app.route('/') 29 | def index(): 30 | return render_template('index.html') 31 | 32 | @app.route('/register',methods=['GET','POST']) 33 | def register(): 34 | if request.method == 'POST': 35 | # handle request 36 | name = request.form['name'] 37 | email = request.form['email'] 38 | password = request.form['password'] 39 | 40 | new_user = User(name=name,email=email,password=password) 41 | db.session.add(new_user) 42 | db.session.commit() 43 | return redirect('/login') 44 | 45 | 46 | 47 | return render_template('register.html') 48 | 49 | @app.route('/login',methods=['GET','POST']) 50 | def login(): 51 | if request.method == 'POST': 52 | email = request.form['email'] 53 | password = request.form['password'] 54 | 55 | user = User.query.filter_by(email=email).first() 56 | 57 | if user and user.check_password(password): 58 | session['email'] = user.email 59 | return redirect('/dashboard') 60 | else: 61 | return render_template('login.html',error='Invalid user') 62 | 63 | return render_template('login.html') 64 | 65 | 66 | @app.route('/dashboard') 67 | def dashboard(): 68 | if session['email']: 69 | user = User.query.filter_by(email=session['email']).first() 70 | return render_template('dashboard.html',user=user) 71 | 72 | return redirect('/login') 73 | 74 | @app.route('/logout') 75 | def logout(): 76 | session.pop('email',None) 77 | return redirect('/login') 78 | 79 | if __name__ == '__main__': 80 | app.run(debug=True) -------------------------------------------------------------------------------- /dashboard.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kritimyantra/flask-authentication-system/27e872f5476ffdf68f0dc71c9ba3d1180d435786/dashboard.png -------------------------------------------------------------------------------- /instance/database.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kritimyantra/flask-authentication-system/27e872f5476ffdf68f0dc71c9ba3d1180d435786/instance/database.db -------------------------------------------------------------------------------- /login.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kritimyantra/flask-authentication-system/27e872f5476ffdf68f0dc71c9ba3d1180d435786/login.png -------------------------------------------------------------------------------- /register.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kritimyantra/flask-authentication-system/27e872f5476ffdf68f0dc71c9ba3d1180d435786/register.png -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kritimyantra/flask-authentication-system/27e872f5476ffdf68f0dc71c9ba3d1180d435786/requirements.txt -------------------------------------------------------------------------------- /templates/dashboard.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Login page 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 |
16 |
17 |

user dashboard

18 |

Name: {{user.name }}

19 |

Email: {{user.email }}

20 | Logout 21 |
22 | 23 |
24 | 25 |
26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Auth system 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 |
16 |
17 |

Home page

18 | Register 19 | Login 20 |
21 | 22 |
23 | 24 |
25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /templates/login.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Login page 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 |
16 |
17 |

Login form

18 |
19 |
20 | 21 | 22 |
23 |
24 | 25 | 26 |
27 | 28 | 29 | Register 30 |
31 |
32 | 33 |
34 | 35 |
36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /templates/register.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Register page 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 |
16 |
17 |

Register form

18 |
19 |
20 | 21 | 22 |
23 | 24 |
25 | 26 | 27 |
28 | 29 |
30 | 31 | 32 |
33 | 34 | 35 | Login 36 |
37 |
38 |
39 | 40 |
41 | 42 | 43 | 44 | --------------------------------------------------------------------------------