File size: 7,996 Bytes
3f87a4d
 
 
 
b9a1469
3f87a4d
 
 
b9a1469
 
 
 
 
3f87a4d
 
 
 
 
 
 
b9a1469
 
3f87a4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9a1469
 
 
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
from email.message import EmailMessage
from flask import Flask, request, jsonify, render_template
import random
import string
from flask_cors import CORS
import smtplib
import bcrypt
import mysql.connector

app = Flask(__name__)

CORS(app)

mysql = mysql.connector.connect(
    host='sql12.freesqldatabase.com',
    user='sql12653124',
    password='kqM3CPBsqP',
    database='sql12653124',
    port=3306
)

    
@app.route('/feedback', methods=['POST', 'GET'])
def feedback():
    if request.method == 'POST':
        data = request.get_json()

        cursor = mysql.cursor(dictionary=True)
        cursor.execute("INSERT INTO `feedbacks`(`email`, `feedback`) VALUES (%s, %s)",
                       (data.get('email'), data.get('feedback'),))
        mysql.commit()
        cursor.close()

        return jsonify({'success': True})

    if request.method == 'GET':
        cursor = mysql.cursor(dictionary=True)
        cursor.execute("SELECT * FROM `feedbacks`")
        feedback_data = cursor.fetchall()
        mysql.commit()
        cursor.close()

        return jsonify({'success': True, 'data': feedback_data})


@app.route('/register', methods=['POST'])
def register():
    if request.method == 'POST':
        data = request.get_json()
        cursor = mysql.cursor(dictionary=True)
        cursor.execute("SELECT * FROM users WHERE email = %s", (data.get('email'),))
        user = cursor.fetchone()
        mysql.commit()
        cursor.close()

        if user:
            error = 'Email address already in use. Please use a different email address.'
            return jsonify({'success': False, 'message': error})
        else:
            msg = EmailMessage()

            # alphabet = string.ascii_letters + string.digits
            otp = random.randint(100000, 999999)
            print(otp)

            cursor = mysql.cursor(dictionary=True)
            cursor.execute("INSERT INTO `otp`(`mail`, `otp`) VALUES (%s, %s)", (data.get('email'), otp,))
            mysql.commit()
            cursor.close()

            msg["Subject"] = "StoryCircle Verification"
            msg["From"] = "[email protected]"
            msg["To"] = data.get('email')

            html_content = render_template('email.html', name=data.get('name'), otp=otp)
            msg.set_content(html_content, subtype='html')

            with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
                smtp.login('[email protected]', 'njoexkbwuscrwdhf')
                smtp.send_message(msg)

            return jsonify({'success': True})


@app.route('/verify', methods=['POST', 'GET'])
def verify():
    if request.method == 'POST':
        data = request.get_json()
        cursor = mysql.cursor(dictionary=True)
        cursor.execute("SELECT `otp` FROM `otp` WHERE `mail`=%s ORDER BY `id` DESC LIMIT 1", (data.get('email'),))
        system_otp = cursor.fetchone()
        mysql.commit()
        cursor.close()

        if system_otp['otp'] == data.get('otp'):
            cursor = mysql.cursor(dictionary=True)
            password = data.get('password').encode('utf-8')
            hash_password = bcrypt.hashpw(password, bcrypt.gensalt())
            cursor.execute("INSERT INTO `users` (`name`, `email`, `password`) VALUES (%s, %s, %s)",(data.get('name'), data.get('email'), hash_password,))
            mysql.commit()
            cursor.close()
            return jsonify({'success': True})
        else:
            return jsonify({'success': False})


@app.route('/login', methods=['POST', 'GET'])
def login():
    if request.method == 'POST':
        data = request.get_json()
        email = data.get('username')
        password = data.get('password').encode('utf-8')

        cursor = mysql.cursor(dictionary=True)
        cursor.execute("SELECT * FROM `users` WHERE email=%s", (email,))
        user = cursor.fetchone()
        mysql.commit()
        cursor.close()


        if user:
            if bcrypt.hashpw(password, user['password'].encode('utf-8')) == user['password'].encode('utf-8'):
                cursor = mysql.cursor(dictionary=True)
                cursor.execute("INSERT INTO `session`(`id`, `name`, `email`) VALUES (%s, %s, %s)", (user['id'], user['name'], user['email'],))
                mysql.commit()
                cursor.close()


                return jsonify({'login': True, 'message': 'Valid User Login', 'id': user['id'],
                        'name': user['name'], 'email': user['email']})

            else:
                return jsonify({'login': False, 'message': 'Invalid Password'})
        else:
            return jsonify({'login': False})


@app.route('/logincheck', methods=['POST'])
def checklogin():
    # print(session)
    data = request.get_json()
    print(data)

    if data.get('email') == 'Meow':
        return jsonify({'login': False})

    cursor = mysql.cursor(dictionary=True)
    cursor.execute("SELECT `id`, `name`, `email` FROM `session` WHERE `email`= %s ORDER BY `index` DESC LIMIT 1;", (data.get('email'),))
    user = cursor.fetchone()
    cursor.close()

    if user:
        return jsonify({'login': True, 'message': 'Valid User Login', 'id': user['id'],
                        'name': user['name'], 'email': user['email']})

    else:
        return jsonify({'login': False})


@app.route('/forgot', methods=['POST'])
def forgot():
    if request.method == 'POST':
        data = request.get_json()
        print(data)
        cursor = mysql.cursor(dictionary=True)
        cursor.execute("SELECT * FROM users WHERE email = %s", (data.get('username'),))
        user = cursor.fetchone()
        mysql.commit()

        if user:
            msg = EmailMessage()

            otp = random.randint(100000, 999999)

            cursor = mysql.cursor(dictionary=True)
            cursor.execute("INSERT INTO `otp`(`mail`, `otp`) VALUES (%s, %s)", (data.get('username'), otp,))
            mysql.commit()
            cursor.close()

            msg["Subject"] = "StoryCircle Verification"
            msg["From"] = "[email protected]"
            msg["To"] = data.get('username')

            html_content = render_template('pass.html', name=user['name'], otp=otp)
            msg.set_content(html_content, subtype='html')

            with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
                smtp.login('[email protected]', 'njoexkbwuscrwdhf')
                smtp.send_message(msg)

            return jsonify({'success': True})
        else:
            error = 'No such User found. Please Register first.'
            return jsonify(error)


@app.route('/verifyforgot', methods=['POST'])
def verifyforgot():
    if request.method == 'POST':
        data = request.get_json()
        print(data)
        cursor = mysql.cursor(dictionary=True)
        cursor.execute("SELECT `otp` FROM `otp` WHERE `mail`=%s ORDER BY `id` DESC LIMIT 1;", (data.get('username'),))
        system_otp = cursor.fetchone()
        print(system_otp['otp'])
        mysql.commit()
        cursor.close()

        if str(system_otp['otp']) == data.get('otp'):
            return jsonify({'success': True})
        else:
            return jsonify({'success': False})


@app.route('/reset', methods=['POST'])
def reset():
    if request.method == 'POST':
        data = request.get_json()
        password = data.get('password').encode('utf-8')
        hash_password = bcrypt.hashpw(password, bcrypt.gensalt())
        cursor = mysql.cursor(dictionary=True)
        cursor.execute("UPDATE `users` SET `password`= %s WHERE `email`= %s", (hash_password, data.get('username'),))
        mysql.commit()
        cursor.close()
        return jsonify({'success': True})


@app.route('/logout', methods=['POST'])
def logout():
    data = request.get_json()
    cursor = mysql.cursor(dictionary=True)
    cursor.execute("DELETE FROM `session` WHERE `email` = %s ;", (data.get('email'),))
    mysql.commit()
    cursor.close()
    return jsonify({'logout': True})

if __name__ == '__main__':
    app.run()