-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.py
68 lines (58 loc) · 1.62 KB
/
auth.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
import pyrebase
import requests
import json
# Please Follow Detailed Explanation from Readme
# Paste your Friebase Details here
# !!! VERY IMPORTANT !!!
CONFIG = {
"apiKey" : "",
"authDomain" : "",
"databaseURL" : "",
"projectId" : "",
"storageBucket" : "",
"messagingSenderId" : "",
"appId" : ""
}
firebase = pyrebase.initialize_app(CONFIG)
auth = firebase.auth()
db = firebase.database()
db = firebase.database()
def new_user(email, password):
try:
user = auth.create_user_with_email_and_password(email, password)
except requests.HTTPError as e:
error_json = e.args[1]
error = json.loads(error_json)['error']['message']
if error == "EMAIL_EXISTS":
return -1, 'Email Already Exists, Try Again'
else:
return -1, 'Something Went Wrong'
else:
db.child(user['localId']).set('')
return 0, user['localId']
def Login(email, password):
try:
user = auth.sign_in_with_email_and_password(email, password)
except requests.HTTPError as e:
error_json = e.args[1]
error = json.loads(error_json)['error']['message']
if error == "EMAIL_NOT_FOUND":
return -1, 'Email is not registered, Try again'
else:
return -1,'Wrong Password Try again'
else:
return 0, user['localId']
def get_notes(id):
user = db.child(id).get()
if user.each() is None:
return 0, dict()
notes = dict()
for note in user.each():
if note.val() != None:
notes[note.key()] = note.val()
return len(notes), notes
def remove_note(id, note):
db.child(id).child(note).remove()
def add_note(id, note):
noteid = db.child(id).push(note)
return noteid['name']