This repository was archived by the owner on Jan 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
93 lines (81 loc) · 2.91 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
from flask import Flask, redirect, url_for, session, request, render_template
from authlib.integrations.flask_client import OAuth
from flask_mail import Mail, Message
from flask_pymongo import PyMongo
import os
import random
from dotenv import load_dotenv
load_dotenv()
app = Flask(__name__)
app.secret_key = os.getenv("SECRET_KEY")
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = os.getenv("MAIL_USERNAME")
app.config['MAIL_PASSWORD'] = os.getenv("MAIL_PASSWORD")
app.config['MONGO_URI'] = os.getenv("MONGO_URI")
mail = Mail(app)
mongo = PyMongo(app)
oauth = OAuth(app)
google = oauth.register(
name='google',
client_id=os.getenv("GOOGLE_CLIENT_ID"),
client_secret=os.getenv("GOOGLE_CLIENT_SECRET"),
access_token_url='https://oauth2.googleapis.com/token',
authorize_url='https://accounts.google.com/o/oauth2/auth',
api_base_url='https://openidconnect.googleapis.com/v1/',
client_kwargs={'scope': 'openid email profile'},
jwks_uri='https://www.googleapis.com/oauth2/v3/certs'
)
def generate_otp():
return random.randint(100000, 999999)
def send_otp_email(user_email, otp):
msg = Message("Your OTP Code", recipients=[user_email], sender=os.getenv("MAIL_USERNAME"))
msg.body = f"Your OTP code is: {otp}"
mail.send(msg)
@app.route('/')
def index():
user = session.get('user')
if user:
return f'Hello, {user["name"]}! (<a href="/logout">Logout</a>)'
return '<a href="/login">Login with Google</a>'
@app.route('/login')
def login():
return google.authorize_redirect(url_for('authorize', _external=True))
@app.route('/authorize')
def authorize():
token = google.authorize_access_token()
resp = google.get('userinfo', token=token)
user_info = resp.json()
session['user_info'] = user_info
session['user_email'] = user_info['email']
otp = generate_otp()
send_otp_email(user_info['email'], otp)
session['otp'] = otp
return redirect(url_for('verify_otp'))
@app.route('/verify', methods=['GET', 'POST'])
def verify_otp():
if request.method == 'POST':
entered_otp = request.form.get('otp')
if entered_otp == str(session.get('otp')):
user_info = session.get('user_info')
mongo.db.users.insert_one({
'google_id': user_info['sub'],
'name': user_info['name'],
'email': user_info['email'],
'profile_picture': user_info['picture'],
})
session['user'] = user_info
session.pop('otp', None)
return redirect('/')
else:
return "Incorrect OTP. Please try again."
return render_template('verify_otp.html')
@app.route('/logout')
def logout():
session.pop('user', None)
session.pop('otp', None)
session.pop('user_info', None)
return redirect('/')
if __name__ == '__main__':
app.run(debug=True)