File size: 1,172 Bytes
9831707
 
 
 
 
56751ce
9831707
 
 
 
 
56751ce
 
9831707
 
 
56751ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9831707
56751ce
9831707
 
 
 
56751ce
9831707
56751ce
9831707
 
56751ce
 
9831707
56751ce
9831707
 
 
56751ce
9831707
 
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
import random
import jwt
import datetime
import os
from dotenv import load_dotenv
from database import register_user, login_user_db, save_otp, verify_otp_db

load_dotenv()

SECRET_KEY = os.getenv("JWT_SECRET", "fallback_secret")


# ---------------- OTP ----------------
def generate_otp():
    return str(random.randint(100000, 999999))


# ---------------- SIGNUP ----------------
def signup_user(email, password):
    return register_user(email, password)


# ---------------- LOGIN ----------------
def login_user(email, password):
    return login_user_db(email, password)


# ---------------- OTP VERIFY ----------------
def verify_user_otp(email, otp):
    return verify_otp_db(email, otp)


# ---------------- JWT ----------------
def create_jwt(email):

    payload = {
        "email": email,
        "exp": datetime.datetime.utcnow() + datetime.timedelta(hours=1)
    }

    token = jwt.encode(payload, SECRET_KEY, algorithm="HS256")

    return token


# ---------------- VERIFY TOKEN ----------------
def verify_jwt(token):

    try:
        decoded = jwt.decode(token, SECRET_KEY, algorithms=["HS256"])
        return decoded

    except:
        return None