Skip to content

Commit

Permalink
Merge pull request #982 from Real-Dev-Squad/feat/discord-page
Browse files Browse the repository at this point in the history
  • Loading branch information
iamitprakash authored Jan 18, 2025
2 parents 4f6a3e1 + 0a8430c commit f27aac6
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 0 deletions.
58 changes: 58 additions & 0 deletions app/controllers/discord.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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 {
queryParams = ['token'];
@service router;
@service toast;
@tracked discordId =
this.model.externalAccountData.attributes.discordId || '';
@tracked linkStatus = 'not-linked';
@tracked isLinkingInProgress = false;
@tracked consent = false;
@tracked token = '';

async model() {
this.token = this.paramsFor('discord').token;
}
@action setConsent() {
this.consent = !this.consent;
}

@action async linkDiscordAccount() {
if (!this.consent) {
this.toast.error(
'Please provide consent by checking the checkbox',
'ERROR',
TOAST_OPTIONS,
);
return;
}

try {
this.isLinkingInProgress = true;

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

this.linkStatus = response.status === 204 ? 'linked' : 'failure';
} catch (error) {
this.linkStatus = 'failure';
console.error('Failed to link Discord account:', error.message);
} finally {
this.isLinkingInProgress = 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');
});
65 changes: 65 additions & 0 deletions app/routes/discord.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
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() {
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',
},
);

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
) {
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 };
}
}
}
4 changes: 4 additions & 0 deletions app/templates/disocrd.hbs
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>
12 changes: 12 additions & 0 deletions app/utils/redirect-auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { AUTH } from '../constants/urls';

export default function redirectAuth() {
let authUrl = AUTH.GITHUB_SIGN_IN;
if (typeof window !== 'undefined') {
const url = new URL(authUrl);
const searchParams = new URLSearchParams(url.search);
searchParams.set('redirectURL', window.location.href);
url.search = searchParams.toString();
window.location.href = url.toString();
}
}
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);
});
});

0 comments on commit f27aac6

Please sign in to comment.