Skip to content

Commit

Permalink
chore: refactored env calling const on api
Browse files Browse the repository at this point in the history
  • Loading branch information
SamCaliman committed Jul 27, 2024
1 parent 220e1b6 commit 0dd58a5
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 55 deletions.
37 changes: 20 additions & 17 deletions api/src/routes/implementations/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,41 @@ import { ACError, type RouteContext, Result, successfulAuthentication } from 'ae
import {OAuth} from '../../oauth.js'

export const github = async(context: RouteContext)=>{

const {
GITHUB_CLIENT_ID,
GITHUB_CLIENT_SECRET,
GITHUB_USER_URL,
GITHUB_TOKEN_URL,
GITHUB_REDIRECT_URI,
} = process.env

if(
!process.env.GITHUB_CLIENT_ID
!GITHUB_CLIENT_ID
||
!process.env.GITHUB_CLIENT_SECRET
!GITHUB_CLIENT_SECRET
||
!process.env.GITHUB_USER_URL
!GITHUB_USER_URL
||
!process.env.GITHUB_TOKEN_URL
!GITHUB_TOKEN_URL
||
!process.env.GITHUB_REDIRECT_URI
!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
GITHUB_CLIENT_ID,
GITHUB_CLIENT_SECRET,
GITHUB_TOKEN_URL,
GITHUB_REDIRECT_URI
) //swap code for access token

const gitTempUser = await OAuth.fetchUser(
gitTempToken.access_token,
USER_URL,
CLIENT_ID,
GITHUB_USER_URL,
GITHUB_CLIENT_ID,
"Accept: application/vnd.github+json"
) // get github user data

Expand Down
43 changes: 23 additions & 20 deletions api/src/routes/implementations/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,41 @@ import { ACError, type RouteContext, Result, successfulAuthentication } from 'ae
import {OAuth} from '../../oauth.js'

export const google = async(context: RouteContext)=>{
if(
!process.env.GOOGLE_CLIENT_ID

const {
GOOGLE_CLIENT_ID,
GOOGLE_CLIENT_SECRET,
GOOGLE_USER_URL,
GOOGLE_TOKEN_URL,
GOOGLE_REDIRECT_URI,
} = process.env

if(
!GOOGLE_CLIENT_ID
||
!process.env.GOOGLE_CLIENT_SECRET
!GOOGLE_CLIENT_SECRET
||
!process.env.GOOGLE_USER_URL
!GOOGLE_USER_URL
||
!process.env.GOOGLE_TOKEN_URL
!GOOGLE_TOKEN_URL
||
!process.env.GOOGLE_REDIRECT_URI
){
!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
GOOGLE_CLIENT_ID,
GOOGLE_CLIENT_SECRET,
GOOGLE_TOKEN_URL,
GOOGLE_REDIRECT_URI
) //swap code for access token

const googleTempUser = await OAuth.fetchUser(
googleTempToken.access_token ,
USER_URL,
CLIENT_ID,
GOOGLE_USER_URL,
GOOGLE_CLIENT_ID,
'json'
) // get google user data

Expand Down
38 changes: 20 additions & 18 deletions api/src/routes/implementations/twitch.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
import { ACError, type RouteContext, Result, successfulAuthentication } from 'aeria'
import {OAuth} from '../../oauth.js'


export const twitch = async(context:RouteContext)=>{

const {
TWITCH_CLIENT_ID,
TWITCH_CLIENT_SECRET,
TWITCH_USER_URL,
TWITCH_TOKEN_URL,
TWITCH_REDIRECT_URI,
} = process.env

if(
!process.env.TWITCH_CLIENT_ID
!TWITCH_CLIENT_ID
||
!process.env.TWITCH_CLIENT_SECRET
!TWITCH_CLIENT_SECRET
||
!process.env.TWITCH_USER_URL
!TWITCH_USER_URL
||
!process.env.TWITCH_TOKEN_URL
!TWITCH_TOKEN_URL
||
!process.env.TWITCH_REDIRECT_URI
!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
TWITCH_CLIENT_ID,
TWITCH_CLIENT_SECRET,
TWITCH_TOKEN_URL,
TWITCH_REDIRECT_URI
) //swap code for access token

const twitchTempUser = await OAuth.fetchUser(
twitchTempToken.access_token ,
USER_URL,
CLIENT_ID,
TWITCH_USER_URL,
TWITCH_CLIENT_ID,
'application/vnd.twitchtv.v5+json'
) // get twitch user data

Expand Down

0 comments on commit 0dd58a5

Please sign in to comment.