Skip to content

Commit d053f49

Browse files
Merge pull request #33 from nullinside-development-group/feature/TwitchBotAuth
Feature/twitch bot auth
2 parents b3e70f1 + 7dc6e40 commit d053f49

File tree

3 files changed

+49
-31
lines changed

3 files changed

+49
-31
lines changed

src/src/app/app.routes.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
import { Routes } from '@angular/router';
2-
import { NotFoundComponent } from "./view/not-found/not-found.component";
3-
import { LoginLandingComponent } from "./view/login-landing/login-landing.component";
4-
import { HomeComponent } from "./view/home/home.component";
5-
import { authGuard } from "./middleware/auth.guard";
6-
import { VmManagerComponent } from './view/vm-manager/vm-manager.component';
7-
import { LoginComponent } from './view/login/login.component';
8-
import { TwitchBotIndexComponent } from './view/twitch/twitch-bot-index/twitch-bot-index.component';
9-
import { TwitchBotConfigComponent } from './view/twitch/twitch-bot-config/twitch-bot-config.component';
10-
import { ImdbSearchComponent } from './view/imdb-search/imdb-search.component';
1+
import {Routes} from '@angular/router';
2+
import {NotFoundComponent} from "./view/not-found/not-found.component";
3+
import {LoginLandingComponent} from "./view/login-landing/login-landing.component";
4+
import {HomeComponent} from "./view/home/home.component";
5+
import {authGuard} from "./middleware/auth.guard";
6+
import {VmManagerComponent} from './view/vm-manager/vm-manager.component';
7+
import {LoginComponent} from './view/login/login.component';
8+
import {TwitchBotIndexComponent} from './view/twitch/twitch-bot-index/twitch-bot-index.component';
9+
import {TwitchBotConfigComponent} from './view/twitch/twitch-bot-config/twitch-bot-config.component';
10+
import {ImdbSearchComponent} from './view/imdb-search/imdb-search.component';
1111

1212
export const routes: Routes = [
1313
{path: '', component: HomeComponent},
1414
{path: 'user/auth', component: LoginComponent},
1515
{path: 'user/login', component: LoginLandingComponent},
1616
{path: 'home', component: HomeComponent},
1717
{path: 'vm-admin', component: VmManagerComponent, canActivate: [authGuard]},
18-
{path: 'twitch-bot/index', component: TwitchBotIndexComponent},
18+
{path: 'twitch-bot', component: TwitchBotIndexComponent},
1919
{path: 'twitch-bot/config', component: TwitchBotConfigComponent},
2020
{path: 'imdb-search', component: ImdbSearchComponent},
2121
{path: '**', component: NotFoundComponent},

src/src/app/view/home/home.component.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import { Component, OnInit } from '@angular/core';
2-
import { LogoComponent } from "../../common/components/logo/logo.component";
3-
import { NullinsideService } from "../../service/nullinside.service";
4-
import { VM_ADMIN } from "../../common/constants";
5-
import { WebsiteApp } from "../../common/interface/website-app";
6-
import { Router } from '@angular/router';
7-
import { MatAnchor, MatButton } from '@angular/material/button';
8-
import { StandardBannerComponent } from '../../common/components/standard-banner/standard-banner.component';
9-
import { LoadingIconComponent } from "../../common/components/loading-icon/loading-icon.component";
10-
import { catchError, forkJoin, Observable, of } from "rxjs";
11-
import { UserRolesResponse } from "../../common/interface/user-roles-response";
1+
import {Component, OnInit} from '@angular/core';
2+
import {LogoComponent} from "../../common/components/logo/logo.component";
3+
import {NullinsideService} from "../../service/nullinside.service";
4+
import {VM_ADMIN} from "../../common/constants";
5+
import {WebsiteApp} from "../../common/interface/website-app";
6+
import {Router} from '@angular/router';
7+
import {MatAnchor, MatButton} from '@angular/material/button';
8+
import {StandardBannerComponent} from '../../common/components/standard-banner/standard-banner.component';
9+
import {LoadingIconComponent} from "../../common/components/loading-icon/loading-icon.component";
10+
import {catchError, forkJoin, Observable, of} from "rxjs";
11+
import {UserRolesResponse} from "../../common/interface/user-roles-response";
1212

1313
@Component({
1414
selector: 'app-home',
@@ -39,7 +39,7 @@ export class HomeComponent implements OnInit {
3939
{
4040
displayName: 'Twitch Bot',
4141
description: 'The nullinside anti-bot Twitch bot.',
42-
url: 'twitch-bot/index',
42+
url: 'twitch-bot',
4343
params: undefined
4444
}
4545
];

src/src/app/view/twitch/twitch-bot-config/twitch-bot-config.component.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {Component, OnDestroy, OnInit} from '@angular/core';
22
import {LogoComponent} from '../../../common/components/logo/logo.component';
33
import {MatButton} from '@angular/material/button';
44
import {NullinsideTwitchBotService} from "../../../service/nullinside-twitch-bot.service";
5-
import {ActivatedRoute, ParamMap} from "@angular/router";
5+
import {ActivatedRoute, ParamMap, Router} from "@angular/router";
66
import {Errors} from "../../login-landing/errors";
77
import {HttpErrorResponse} from "@angular/common/http";
88
import {NullinsideService} from "../../../service/nullinside.service";
@@ -13,6 +13,7 @@ import {MatCheckbox} from "@angular/material/checkbox";
1313
import {TwitchBotFaqComponent} from "../twitch-bot-faq/twitch-bot-faq.component";
1414
import {MatSnackBar} from "@angular/material/snack-bar";
1515
import {FormsModule} from "@angular/forms";
16+
import {Location} from "@angular/common";
1617

1718
@Component({
1819
selector: 'app-twitch-bot-config',
@@ -41,6 +42,8 @@ export class TwitchBotConfigComponent implements OnInit, OnDestroy {
4142
constructor(private twitchBotApi: NullinsideTwitchBotService,
4243
private api: NullinsideService,
4344
private snackBar: MatSnackBar,
45+
private location: Location,
46+
private router: Router,
4447
private route: ActivatedRoute) {
4548
}
4649

@@ -67,16 +70,30 @@ export class TwitchBotConfigComponent implements OnInit, OnDestroy {
6770
return;
6871
}
6972

73+
// Get the token, if there isn't one then they didn't get here by hitting the login button. Lets send them back
74+
// to the login page if that's the case. It'll help us keep people synced on updates the permissions we request
75+
// from their tokens.
7076
const token = params.get('token');
71-
if (token) {
72-
localStorage.setItem('auth-token', token);
73-
this.api.validateToken(token).subscribe({
74-
error: (_: HttpErrorResponse) => {
75-
this.onLoginFailed();
76-
}
77-
});
77+
if (!token) {
78+
this.router.navigate(['twitch-bot']);
79+
return;
7880
}
7981

82+
// Save the token and make sure its valid.
83+
localStorage.setItem('auth-token', token);
84+
this.api.validateToken(token).subscribe({
85+
error: (_: HttpErrorResponse) => {
86+
this.onLoginFailed();
87+
}
88+
});
89+
90+
// Kind of a workaround to remove the token from the query parameters in the url. The reason for this is
91+
// if someone wants to bookmark the page we don't want to leave that in there. A different site layout will
92+
// make this unnecessary.
93+
const url = this.router.createUrlTree([], {relativeTo: this.route}).toString();
94+
this.location.go(url);
95+
96+
// Check if the bot account is modded. If it isn't, we can offer to mod it.
8097
this.twitchBotApi.getIsMod().subscribe({
8198
next: response => {
8299
this.botIsMod = response.isMod;
@@ -88,6 +105,7 @@ export class TwitchBotConfigComponent implements OnInit, OnDestroy {
88105
}
89106
});
90107

108+
// Get the person's existing configuration.
91109
this.twitchBotApi.getConfig().subscribe({
92110
next: response => {
93111
this.botEnabled = response.isEnabled;

0 commit comments

Comments
 (0)