Discord Provider #601
XdGoldenTigerOfficial
started this conversation in
General
Replies: 1 comment 1 reply
-
I just finished setting up my own service and guard for my dashboard. If you guys want to setup a Discord auth, Here is my code: Auth Guard export class AuthGuard {
constructor(private authService: AuthService, private router: Router) {}
async canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Promise<boolean | Observable<boolean>> {
if (this.authService.isLoggedIn() !== true) {
this.router.navigate(['/']);
}
if (this.authService.isUserExpired === true) {
this.authService.doLogout();
}
return true;
}
} Auth Service: export class AuthService {
constructor(public router: Router) {}
loginDiscord() {
window.location.href = `https://discord.com/api/oauth2/authorize?client_id=${CLIENT_ID}&redirect_uri=${REDIRECT_URI}&response_type=code&scope=identify`;
}
setUserData(data: any) {
userData = data;
localStorage.setItem('LoggedIn', JSON.stringify(userData));
}
getUserData() {
return JSON.parse(localStorage.getItem('LoggedIn') || '{}');
}
loginCallback() {
this.router.navigate(['/servers']);
}
doLogout() {
let removeToken = localStorage.removeItem('LoggedIn');
if (removeToken == null) {
this.router.navigate(['/']);
}
}
get isUserExpired(): boolean {
return false; // Still haven't set this up yet!
}
isLoggedIn() {
var userLoggedInData = localStorage.getItem('LoggedIn');
return userLoggedInData !== null ? true : false;
}
} Callback Component: export class CallbackComponent implements OnInit {
constructor(
private authService: AuthService,
private route: ActivatedRoute
) {}
async ngOnInit() {
var code = '';
this.route.queryParams.subscribe((params) => {
code = params['code'];
});
const params = new URLSearchParams({
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
code: code,
grant_type: 'authorization_code',
redirect_uri: REDIRECT_URI,
});
try {
const tokenRes = await axios.post(
'https://discordapp.com/api/oauth2/token',
params,
{}
);
const token = tokenRes.data.access_token;
const userRes = await axios.get(`https://discord.com/api/v6/users/@me`, {
headers: {
Authorization: `Bearer ${token}`,
},
});
this.authService.setUserData(userRes.data);
this.authService.loginCallback();
} catch (err) {}
}
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey, Has anyone here created a provider for Discord auth?
If so, Could you please post an example of how I would need to set a custom provider up?
I am new to angularx-social-login and the whole auth thing with Angular.
I am really wanting to setup a Dashboard for a bot, But dont know how to setup a Discord login, I found this package, But doesnt have the provider I need.
Beta Was this translation helpful? Give feedback.
All reactions