Skip to content

Commit

Permalink
feat: finished implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
SamCaliman committed Jul 4, 2024
1 parent eca48ed commit 5752e72
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 103 deletions.
66 changes: 33 additions & 33 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"aeria": "^0.0.135",
"aeria": "^0.0.137",
"aeria-sdk": "^0.0.109"
}
}
73 changes: 32 additions & 41 deletions api/src/routes/github.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { createRouter } from 'aeria'
import { authenticate } from 'aeria-sdk';
import { createRouter, HTTPStatus, Result, successfulAuthentication } from 'aeria'

export const githubRouter = createRouter()

async function exangeCodeForAccessToken(code: string) {
async function exchangeCodeForAccessToken(code: string) {

const githubURL = 'https://github.com/login/oauth/access_token'
const CLIENT_ID = process.env.GITHUB_CLIENT_ID
if(CLIENT_ID === undefined || CLIENT_ID === null){
return console.log('no clientIDFound')

if(!process.env.GITHUB_CLIENT_ID || !process.env.GITHUB_CLIENT_SECRET){
throw new Error('INVALID ENV FILES')
}

const CLIENT_ID = process.env.GITHUB_CLIENT_ID
const CLIENT_SECRET = process.env.GITHUB_CLIENT_SECRET
if(CLIENT_SECRET === undefined || CLIENT_SECRET === null){
return console.log('no clientSECRETFound')
}

const body = {
code: code,
grant_type: 'authorization_code',
Expand Down Expand Up @@ -42,41 +42,32 @@ async function fetchUser(token: string) {
}

githubRouter.POST('/githubAuth', async(context)=>{
try {
const gtiTempToken = await exangeCodeForAccessToken(context.request.payload.code)
const gitTempUser = await fetchUser(gtiTempToken.access_token)
const gitTempToken = await exchangeCodeForAccessToken(context.request.payload.code)
const gitTempUser = await fetchUser(gitTempToken.access_token)

const { error: userError ,result: userResult } = await context.collections.user.functions.get({
filters: {
github_id: gitTempUser.id.toString(),
const { error: userError ,result: user } = await context.collections.user.functions.get({
filters: {
github_id: gitTempUser.id.toString(),
},
})

if(userError){
const gitUserId = gitTempUser.id
const { error: userInsertError, result: userInsertResult } = await context.collections.user.functions.insert({
what: {
name: gitTempUser.login,
active: true,
github_id: gitUserId.toString(),
roles: ['root'],
email: `${gitTempUser.login}@user.github.com`,
},
})

console.log(gitTempUser.id)

if(userError){
const gitUserId = gitTempUser.id
const { error: userInsertError, result: userInsertResult } = await context.collections.user.functions.insert({
what: {
name: gitTempUser.login,
active: true,
github_id: gitUserId.toString(),
roles: ['root'],
email: `${gitTempUser.login}@user.github.com`,
},
})
if (userInsertError){
return console.log(userInsertError)
}
console.log(userInsertResult)
}
if (userResult){
//console.log(gitTempUser)
console.log(userResult)
if (userInsertError){
return context.error(HTTPStatus.InternalServerError, {code: userInsertError.code})
}
authenticate()
//console.log(gitTempUser.id)
} catch (error) {
console.log('err', error)
return Result.result(await successfulAuthentication(userInsertResult._id, context))
}
if (user){
return Result.result(await successfulAuthentication(user._id, context))
}
})
34 changes: 17 additions & 17 deletions web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
"license": "ISC",
"dependencies": {
"@aeria-ui/i18n-en": "^0.0.3",
"aeria-app-layout": "^0.0.40",
"aeria-app-layout": "^0.0.41",
"aeria-sdk": "^0.0.109",
"aeria-ui": "^0.0.78"
"aeria-ui": "^0.0.80"
},
"devDependencies": {
"autoprefixer": "^10.4.19",
Expand Down
4 changes: 1 addition & 3 deletions web/src/pages/githubAuth.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@
async function githubAuth() {
const CLIENT_ID = import.meta.env.VITE_GITHUB_CLIENT_ID
const GITHUB_URL = 'https://github.com/login/oauth/authorize'
const params = {
response_type: 'code',
scope: 'user',
client_id: CLIENT_ID,
redirect_uri: 'http://localhost:8080/redirect'
}
console.log(`${GITHUB_URL}`+'?'+`${new URLSearchParams(params).toString()}`)
if(CLIENT_ID){
window.open(`${GITHUB_URL}`+'?'+`${new URLSearchParams(params).toString()}`)
window.open(`${GITHUB_URL}`+'?'+`${new URLSearchParams(params).toString()}`, '_self')
}
}
Expand Down
Loading

0 comments on commit 5752e72

Please sign in to comment.