Skip to content

Commit

Permalink
Feat: Add User
Browse files Browse the repository at this point in the history
  • Loading branch information
KDwevedi committed May 27, 2023
1 parent e64f2a5 commit 1f562ef
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#all files relating to python vitual env
/.venv
#environment variables and cache
*.env
*/__pycache__/*
Binary file added __pycache__/convert_to_json.cpython-311.pyc
Binary file not shown.
Binary file added __pycache__/db.cpython-311.pyc
Binary file not shown.
Binary file added __pycache__/main.cpython-311.pyc
Binary file not shown.
39 changes: 39 additions & 0 deletions db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import os
from typing import Any
from supabase import create_client, Client
import dotenv

dotenv.load_dotenv(".env")

url: str = os.getenv("SUPABASE_URL")
key: str = os.getenv("SUPABASE_KEY")

class SupabaseInterface:
def __init__(self, url, key) -> None:
self.supabase_url = url
self.supabase_key = key
self.client: Client = create_client(self.supabase_url, self.supabase_key)

def add_user(self, userdata):

data = self.client.table("users").insert(userdata).execute()
print(data.data)
return data

def user_exists(self, discord_id):
data = self.client.table("users").select("*").eq("discord_id", discord_id).execute()
if len(data.data)>0:
return True
else:
return False



tester = SupabaseInterface(url,key)
# tester.add_user({
# "discord_id": 476285280811483140,
# "github_id": 74085496

# })

print(tester.user_exists(476285280811483141))
68 changes: 62 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,78 @@
from flask import Flask
from flask import Flask, redirect, render_template
import requests
from flask import request
import dotenv
import os
from db import SupabaseInterface

dotenv.load_dotenv(".env")

app = Flask(__name__)
app.config['TESTING']= True
app.config['SECRET_KEY']=os.getenv("FLASK_SESSION_KEY")

@app.route("/")
def hello_world():
return "<p>Hello, World!</p>"

@app.route("/register/<discord_id>")
def get_data(discord_id):

@app.route("/already_authenticated")
def isAuthenticated():
return render_template('success.html'), {"Refresh": f'1; url=https://discord.com/channels/{os.getenv("DISCORD_SERVER_ID")}'}

@app.route("/authenticate/<discord_userdata>")
def authenticate(discord_userdata):

redirect_uri = f'{os.getenv("HOST")}/register/{discord_userdata}'
github_auth_url = f'https://github.com/login/oauth/authorize?client_id={os.getenv("GITHUB_CLIENT_ID")}&redirect_uri={redirect_uri}'
return redirect(github_auth_url)

#this is where github calls back to
@app.route("/register/<discord_userdata>")
def register(discord_userdata):
url = os.getenv("SUPABASE_URL")
key = os.getenv("SUPABASE_KEY")


#Extrapolate discord data from callback
#$ sign is being used as separator
[discord_id, discord_username, role] = discord_userdata.split('$')

#Check if the user is registered
supabase_client = SupabaseInterface(url=url, key=key)
if supabase_client.user_exists(discord_id=discord_id):
print('true')
authenticated_url = f'{os.getenv("HOST")}/already_authenticated'
return redirect(authenticated_url)

#get github ID
github_url_for_access_token = 'https://github.com/login/oauth/access_token'
data = {
"client_id": os.getenv("GITHUB_CLIENT_ID"),
"client_secret": os.getenv("GITHUB_CLIENT_SECRET"),
"code": request.args.get("code")
}
header = {
"Accept":"application/json"
}
r = requests.post(github_url_for_access_token, data=data, headers=header)
auth_token = r.json()["access_token"]
user = requests.get("https://api.github.com/user", headers={
"Authorization": f"Bearer {auth_token}"
})
print(user.json())

github_id = user.json()["id"]
github_username = user.json()["login"]


#adding to the database
supabase_client.add_user({
"discord_id": int(discord_id),
"github_id": github_id,
"github_url": f"https://github.com/{github_username}",
"discord_username": discord_username,
"role": role
})


return ''

return render_template('success.html'), {"Refresh": f'1; url=https://discord.com/channels/{os.getenv("DISCORD_SERVER_ID")}'}
34 changes: 34 additions & 0 deletions samples/userAuthenticationResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"login": "KDwevedi",
"id": 74085496,
"node_id": "MDQ6VXNlcjc0MDg1NDk2",
"avatar_url": "https://avatars.githubusercontent.com/u/74085496?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/KDwevedi",
"html_url": "https://github.com/KDwevedi",
"followers_url": "https://api.github.com/users/KDwevedi/followers",
"following_url": "https://api.github.com/users/KDwevedi/following{/other_user}",
"gists_url": "https://api.github.com/users/KDwevedi/gists{/gist_id}",
"starred_url": "https://api.github.com/users/KDwevedi/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/KDwevedi/subscriptions",
"organizations_url": "https://api.github.com/users/KDwevedi/orgs",
"repos_url": "https://api.github.com/users/KDwevedi/repos",
"events_url": "https://api.github.com/users/KDwevedi/events{/privacy}",
"received_events_url": "https://api.github.com/users/KDwevedi/received_events",
"type": "User",
"site_admin": false,
"name": null,
"company": null,
"blog": "",
"location": null,
"email": null,
"hireable": null,
"bio": null,
"twitter_username": null,
"public_repos": 15,
"public_gists": 1,
"followers": 0,
"following": 1,
"created_at": "2020-11-07T03:48:50Z",
"updated_at": "2023-05-20T09:13:17Z"
}
10 changes: 10 additions & 0 deletions templates/success.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Success!</title>
</head>
<body>
<h1>You have been authenticated! Redirecting...</h1>
</body>
</html>
14 changes: 0 additions & 14 deletions vercel.json

This file was deleted.

0 comments on commit 1f562ef

Please sign in to comment.