Skip to content

Commit

Permalink
refactor: route and remove client_id
Browse files Browse the repository at this point in the history
  • Loading branch information
tejaskh3 committed Jan 17, 2025
1 parent a09bd53 commit 89b1b10
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
3 changes: 1 addition & 2 deletions app/constants/urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ export const SOCIALS = {
WHITE_ICON: 'assets/icons/instagram-white.png',
},
};
export const AUTH_URL =
'https://github.com/login/oauth/authorize?client_id=23c78f66ab7964e5ef97';

export const ANKUSH_TWITTER = 'https://twitter.com/ankushdharkar';
export const RDS_TWITTER = 'https://x.com/realdevsquad';
6 changes: 1 addition & 5 deletions app/controllers/discord.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,16 @@ import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';

export default class DiscordController extends Controller {
queryParams = ['token'];
@service router;
@service toast;
@tracked discordId =
this.model.externalAccountData.attributes.discordId || '';
@tracked linkStatus = 'not-linked';
@tracked isLinking = false;
@tracked consent = false;

@tracked token = '';

queryParams = {
token: { refreshModel: true },
};

async model() {
this.token = this.paramsFor('discord').token;
}
Expand Down
30 changes: 18 additions & 12 deletions app/routes/discord.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { TOAST_OPTIONS } from '../constants/toast-options';
export default class DiscordRoute extends Route {
@service router;
@service toast;

queryParams = {
token: { refreshModel: true },
};
Expand All @@ -27,33 +26,40 @@ export default class DiscordRoute extends Route {
credentials: 'include',
},
);

const userResponse = await fetch(
`${APPS.API_BACKEND}/users?profile=true`,
{
credentials: 'include',
},
);

const externalAccountData = await externalAccountResponse.json();
const userData = await userResponse.json();
if (userResponse.status === 401) {
const userData = await userResponse.json();
this.toast.error(userData.message, '', TOAST_OPTIONS);
setTimeout(redirectAuth, 2000);
return { isTokenExpired: true };
}

if (externalAccountResponse.status === 401) {
const externalAccountData = await externalAccountResponse.json();
this.toast.error(externalAccountData.message, '', TOAST_OPTIONS);
return { isTokenExpired: true };
}

if (
userResponse.status === 200 &&
externalAccountResponse.status === 200
) {
return { externalAccountData, userData, isTokenEpired: false };
} else if (userResponse.status === 401) {
this.toast.error(userData.message, '', TOAST_OPTIONS);
setTimeout(redirectAuth, 2000);
} else if (externalAccountResponse.status === 401) {
this.toast.error(externalAccountData.message, '', TOAST_OPTIONS);

return { isTokenExpired: true };
const externalAccountData = await externalAccountResponse.json();
const userData = await userResponse.json();
return { externalAccountData, userData, isTokenExpired: false };
}

throw new Error('Unexpected response status');
} catch (error) {
this.toast.error(error.message, '', TOAST_OPTIONS);
console.error(error.message);
return { isTokenExpired: true, error: error.message };
}
}
}
7 changes: 4 additions & 3 deletions app/utils/redirect-auth.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { AUTH_URL } from '../constants/urls';
import { AUTH } from '../constants/urls';

export default function () {
let authUrl = AUTH_URL;
let authUrl = AUTH.GITHUB_SIGN_IN;
if (typeof window !== 'undefined') {
authUrl = `${authUrl}&state=${window.location.href}`;
const separator = authUrl.includes('?') ? '&' : '?';
authUrl = `${authUrl}${separator}redirectURL=${encodeURIComponent(window.location.href)}`;
}
window.open(authUrl, '_self');
}

0 comments on commit 89b1b10

Please sign in to comment.