-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4bb388e
commit a068768
Showing
8 changed files
with
299 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const exchangeCodeForAccessToken = async(code: string, clientId: string, clientSecret: string, tokenUrl:string, redirectUri:string) => { | ||
const body = { | ||
code: code, | ||
grant_type: 'authorization_code', | ||
client_id: clientId, | ||
client_secret: clientSecret, | ||
redirect_uri: redirectUri | ||
} | ||
const tokenResponse = await fetch(tokenUrl,{ | ||
method: 'POST', | ||
body: new URLSearchParams(body), | ||
headers: { | ||
Accept: 'application/json', | ||
}, | ||
}) | ||
const responseObject = await tokenResponse.json() | ||
return responseObject | ||
} | ||
//get user data with Access Token | ||
|
||
const fetchUser = async(token: string, userUrl: string, clientId:string, accept:string) => { | ||
const userResponse = await fetch(userUrl,{ | ||
method: 'GET', | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
"Accept": accept, | ||
"Client-ID": clientId | ||
}, | ||
}) | ||
const response = await userResponse.json() | ||
if(response.data){ | ||
return response.data[0] | ||
} | ||
return response | ||
} | ||
|
||
export const OAuth = { | ||
exchangeCodeForAccessToken, | ||
fetchUser | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { ACError, type RouteContext, Result, successfulAuthentication } from 'aeria' | ||
import {OAuth} from '../../oauth.js' | ||
|
||
export const github = async(context: RouteContext)=>{ | ||
if( | ||
!process.env.GITHUB_CLIENT_ID | ||
|| | ||
!process.env.GITHUB_CLIENT_SECRET | ||
|| | ||
!process.env.GITHUB_USER_URL | ||
|| | ||
!process.env.GITHUB_TOKEN_URL | ||
|| | ||
!process.env.GITHUB_REDIRECT_URI | ||
){ | ||
throw new Error('INVALID ENV FILES') | ||
} | ||
|
||
const CLIENT_ID = process.env.GITHUB_CLIENT_ID | ||
const CLIENT_SECRET = process.env.GITHUB_CLIENT_SECRET | ||
const USER_URL = process.env.GITHUB_USER_URL | ||
const TOKEN_URL = process.env.GITHUB_TOKEN_URL | ||
const REDIRECT_URI = process.env.GITHUB_REDIRECT_URI | ||
|
||
const gitTempToken = await OAuth.exchangeCodeForAccessToken( | ||
context.request.payload.code, | ||
CLIENT_ID, | ||
CLIENT_SECRET, | ||
TOKEN_URL, | ||
REDIRECT_URI | ||
) //swap code for access token | ||
|
||
const gitTempUser = await OAuth.fetchUser( | ||
gitTempToken.access_token, | ||
USER_URL, | ||
CLIENT_ID, | ||
"Accept: application/vnd.github+json" | ||
) // get github user data | ||
|
||
//checks if there's an user with a github account on the database. | ||
const { error: userError ,result: user } = await context.collections.user.functions.get({ | ||
filters: { | ||
github_id: gitTempUser.id.toString(), | ||
}, | ||
}) | ||
|
||
if(userError){ | ||
//Check what user error returns | ||
switch(userError.code){ | ||
case ACError.ResourceNotFound:{ | ||
//if there's no user on database, create one. | ||
const { error: userInsertError, result: userInsertResult } = await context.collections.user.functions.insert({ | ||
what: { | ||
name: gitTempUser.login, | ||
active: true, | ||
github_id: gitTempUser.id.toString(), | ||
roles: ['root'], | ||
email: `${gitTempUser.login}@user.template.com`, | ||
}, | ||
}) | ||
if (userInsertError){ | ||
return Result.error(userInsertError) | ||
} | ||
//Authenticate if successful, and return result to web | ||
return Result.result(await successfulAuthentication(userInsertResult._id, context)) | ||
} | ||
default: | ||
return Result.error(userError) | ||
} | ||
} | ||
//if user already exists in database just authenticate and return result to web | ||
return Result.result(await successfulAuthentication(user._id, context)) | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { ACError, type RouteContext, Result, successfulAuthentication } from 'aeria' | ||
import {OAuth} from '../../oauth.js' | ||
|
||
export const google = async(context: RouteContext)=>{ | ||
if( | ||
!process.env.GOOGLE_CLIENT_ID | ||
|| | ||
!process.env.GOOGLE_CLIENT_SECRET | ||
|| | ||
!process.env.GOOGLE_USER_URL | ||
|| | ||
!process.env.GOOGLE_TOKEN_URL | ||
|| | ||
!process.env.GOOGLE_REDIRECT_URI | ||
){ | ||
throw new Error('INVALID ENV FILES') | ||
} | ||
|
||
const CLIENT_ID = process.env.GOOGLE_CLIENT_ID | ||
const CLIENT_SECRET = process.env.GOOGLE_CLIENT_SECRET | ||
const USER_URL = process.env.GOOGLE_USER_URL | ||
const TOKEN_URL = process.env.GOOGLE_TOKEN_URL | ||
const REDIRECT_URI = process.env.GOOGLE_REDIRECT_URI | ||
|
||
const googleTempToken = await OAuth.exchangeCodeForAccessToken( | ||
context.request.payload.code, | ||
CLIENT_ID, | ||
CLIENT_SECRET, | ||
TOKEN_URL, | ||
REDIRECT_URI | ||
) //swap code for access token | ||
|
||
const googleTempUser = await OAuth.fetchUser( | ||
googleTempToken.access_token , | ||
USER_URL, | ||
CLIENT_ID, | ||
'json' | ||
) // get google user data | ||
|
||
//checks if there's an user with a google account on the database. | ||
const { error: userError ,result: user } = await context.collections.user.functions.get({ | ||
filters: { | ||
google_id: googleTempUser.sub.toString(), | ||
}, | ||
}) | ||
|
||
if(userError){ | ||
//Check what user error return | ||
switch(userError.code){ | ||
case ACError.ResourceNotFound:{ | ||
//if there's no user on database, create one. | ||
const { error: userInsertError, result: userInsertResult } = await context.collections.user.functions.insert({ | ||
what: { | ||
name: googleTempUser.name, | ||
active: true, | ||
google_id: googleTempUser.sub.toString(), | ||
roles: ['root'], | ||
email: `${googleTempUser.name}@user.template.com`, | ||
}, | ||
}) | ||
if (userInsertError){ | ||
return Result.error(userInsertError) | ||
} | ||
//Authenticate if successful, and return result to web | ||
return Result.result(await successfulAuthentication(userInsertResult._id, context)) | ||
} | ||
default: | ||
return Result.error(userError) | ||
} | ||
} | ||
//if user already exists in database just authenticate and return result to web | ||
return Result.result(await successfulAuthentication(user._id, context)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { ACError, type RouteContext, Result, successfulAuthentication } from 'aeria' | ||
import {OAuth} from '../../oauth.js' | ||
|
||
|
||
export const twitch = async(context:RouteContext)=>{ | ||
if( | ||
!process.env.TWITCH_CLIENT_ID | ||
|| | ||
!process.env.TWITCH_CLIENT_SECRET | ||
|| | ||
!process.env.TWITCH_USER_URL | ||
|| | ||
!process.env.TWITCH_TOKEN_URL | ||
|| | ||
!process.env.TWITCH_REDIRECT_URI | ||
){ | ||
throw new Error('INVALID ENV FILES') | ||
} | ||
|
||
const CLIENT_ID = process.env.TWITCH_CLIENT_ID | ||
const CLIENT_SECRET = process.env.TWITCH_CLIENT_SECRET | ||
const USER_URL = process.env.TWITCH_USER_URL | ||
const TOKEN_URL = process.env.TWITCH_TOKEN_URL | ||
const REDIRECT_URI = process.env.TWITCH_REDIRECT_URI | ||
|
||
const twitchTempToken = await OAuth.exchangeCodeForAccessToken( | ||
context.request.payload.code, | ||
CLIENT_ID, | ||
CLIENT_SECRET, | ||
TOKEN_URL, | ||
REDIRECT_URI | ||
) //swap code for access token | ||
|
||
const twitchTempUser = await OAuth.fetchUser( | ||
twitchTempToken.access_token , | ||
USER_URL, | ||
CLIENT_ID, | ||
'application/vnd.twitchtv.v5+json' | ||
) // get twitch user data | ||
|
||
//checks if there's an user with a twitch account on the database. | ||
const { error: userError ,result: user } = await context.collections.user.functions.get({ | ||
filters: { | ||
twitch_id: twitchTempUser.id.toString(), | ||
}, | ||
}) | ||
|
||
if(userError){ | ||
//Check what user error returns | ||
switch(userError.code){ | ||
case ACError.ResourceNotFound:{ | ||
//if there's no user on database, create one. | ||
const { error: userInsertError, result: userInsertResult } = await context.collections.user.functions.insert({ | ||
what: { | ||
name: twitchTempUser.login, | ||
active: true, | ||
twitch_id: twitchTempUser.id.toString(), | ||
roles: ['root'], | ||
email: `${twitchTempUser.login}@user.template.com`, | ||
}, | ||
}) | ||
if (userInsertError){ | ||
return Result.error(userInsertError) | ||
} | ||
//Authenticate if successful, and return result to web | ||
return Result.result(await successfulAuthentication(userInsertResult._id, context)) | ||
} | ||
default: | ||
return Result.error(userError) | ||
} | ||
} | ||
//if user already exists in database just authenticate and return result to web | ||
return Result.result(await successfulAuthentication(user._id, context)) | ||
} |
Oops, something went wrong.