Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[My Site Migration] feat: add discord route #982

Merged
merged 3 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/constants/urls.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export const SOCIALS = {
WHITE_ICON: 'assets/icons/instagram-white.png',
},
};

export const AUTH_URL =
'https://github.com/login/oauth/authorize?client_id=23c78f66ab7964e5ef97';
tejaskh3 marked this conversation as resolved.
Show resolved Hide resolved
export const ANKUSH_TWITTER = 'https://twitter.com/ankushdharkar';
export const RDS_TWITTER = 'https://x.com/realdevsquad';
65 changes: 65 additions & 0 deletions app/controllers/discord.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import Controller from '@ember/controller';
import { TOAST_OPTIONS } from '../constants/toast-options';
import { APPS } from '../constants/urls';
import { inject as service } from '@ember/service';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';

export default class DiscordController extends Controller {
@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;
}
@action setConsent() {
this.consent = !this.consent;
}

@action async linkDiscordAccount() {
try {
this.isLinking = true;

if (this.consent) {
const response = await fetch(
`${APPS.API_BACKEND}/external-accounts/link/${this.token}`,
{
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
},
credentials: 'include',
},
);

if (response.status === 204) {
this.linkStatus = 'linked';
} else {
this.linkStatus = 'failure';
}
} else {
this.toast.error(
'Please provide the consent by clicking the checkbox!',
'ERROR',
TOAST_OPTIONS,
);
}
} catch (error) {
this.linkStatus = 'failure';
console.error(error.message);
} finally {
this.isLinking = false;
}
}
}
1 change: 1 addition & 0 deletions app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ Router.map(function () {
this.route('subscribe');
this.route('login');
this.route('identity');
this.route('discord');
});
59 changes: 59 additions & 0 deletions app/routes/discord.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import Route from '@ember/routing/route';
import { APPS } from '../constants/urls';
import { inject as service } from '@ember/service';
import redirectAuth from '../utils/redirect-auth';
import { TOAST_OPTIONS } from '../constants/toast-options';

export default class DiscordRoute extends Route {
@service router;
@service toast;

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

beforeModel(transition) {
if (!transition.to.queryParams.dev) {
this.router.transitionTo('page-not-found');
}
}

async model() {
tejaskh3 marked this conversation as resolved.
Show resolved Hide resolved
try {
const token = this.paramsFor('discord').token;
const externalAccountResponse = await fetch(
`${APPS.API_BACKEND}/external-accounts/${token}`,
{
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 === 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 };
}
} catch (error) {
this.toast.error(error.message, '', TOAST_OPTIONS);
console.error(error.message);
}
}
}
4 changes: 4 additions & 0 deletions app/templates/disocrd.hbs
tejaskh3 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div>
{{!-- TODO: add discord hbs file in next PRs--}}
<h1>Discord Page</h1>
</div>
9 changes: 9 additions & 0 deletions app/utils/redirect-auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { AUTH_URL } from '../constants/urls';

export default function () {
let authUrl = AUTH_URL;
if (typeof window !== 'undefined') {
authUrl = `${authUrl}&state=${window.location.href}`;
}
window.open(authUrl, '_self');
}
11 changes: 11 additions & 0 deletions tests/unit/routes/discord-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { module, test } from 'qunit';
import { setupTest } from 'website-www/tests/helpers';

module('Unit | Route | discord', function (hooks) {
setupTest(hooks);

test('it exists', function (assert) {
let route = this.owner.lookup('route:discord');
assert.ok(route);
});
});
Loading