|
1 | 1 | import {Component, inject, OnInit} from '@angular/core';
|
2 | 2 | import {ActivatedRoute, ParamMap} from "@angular/router";
|
3 |
| -import {Errors} from "../login-landing/errors"; |
| 3 | +import {Errors} from "./errors"; |
4 | 4 | import {HttpErrorResponse} from "@angular/common/http";
|
5 |
| -import {LoadingIconComponent} from "../../common/components/loading-icon/loading-icon.component"; |
6 | 5 | import {LogoComponent} from "../../common/components/logo/logo.component";
|
7 |
| -import {OAuth} from "../../common/interface/oauth"; |
8 |
| -import {MatButton} from "@angular/material/button"; |
9 |
| -import {CdkCopyToClipboard} from "@angular/cdk/clipboard"; |
10 |
| -import {interval, Subscription} from "rxjs"; |
11 | 6 |
|
12 | 7 | @Component({
|
13 | 8 | selector: 'app-login-landing-desktop',
|
14 | 9 | imports: [
|
15 |
| - LoadingIconComponent, |
16 |
| - LogoComponent, |
17 |
| - MatButton, |
18 |
| - CdkCopyToClipboard |
| 10 | + LogoComponent |
19 | 11 | ],
|
20 | 12 | templateUrl: './login-landing-desktop.component.html',
|
21 | 13 | styleUrl: './login-landing-desktop.component.scss'
|
22 | 14 | })
|
23 | 15 | export class LoginLandingDesktopComponent implements OnInit {
|
24 | 16 | private route = inject(ActivatedRoute);
|
25 |
| - private timer: Subscription | undefined; |
26 |
| - private desktopData: string | undefined; |
27 | 17 |
|
28 |
| - loggedIn: boolean = false; |
29 | 18 | error: string = '';
|
30 |
| - oAuth: OAuth | null = null; |
31 |
| - loading: boolean = true; |
32 | 19 |
|
33 | 20 | ngOnInit(): void {
|
34 | 21 | this.route.queryParamMap.subscribe({
|
35 | 22 | next: (params: ParamMap) => {
|
| 23 | + // Handle any errors |
36 | 24 | const error = params.get('error');
|
37 | 25 | if (null !== error) {
|
38 | 26 | const errorNum = +error;
|
39 |
| - if (Errors.TWITCH_ACCOUNT_HAS_NO_EMAIL === errorNum) { |
40 |
| - this.onLoginFailed('Your Twitch account must have a valid e-mail address, please add one and try again') |
41 |
| - } else if (Errors.TWITCH_ERROR_WITH_TOKEN === errorNum) { |
42 |
| - this.onLoginFailed('Twitch failed to give us a valid token, please try again') |
43 |
| - } else { |
44 |
| - this.onLoginFailed('Sorry we did something wrong trying to log you in, please try again') |
| 27 | + if (Errors.INTERNAL_ERROR === errorNum) { |
| 28 | + this.onLoginFailed() |
| 29 | + } else if (Errors.TWITCH_FAILED_TO_GENERATE_TOKEN === errorNum) { |
| 30 | + this.onLoginFailed('Received error from twitch, it may be down temporarily') |
45 | 31 | }
|
46 | 32 |
|
47 | 33 | return;
|
48 | 34 | }
|
49 |
| - |
50 |
| - const oAuth = { |
51 |
| - bearer: params.get('bearer'), |
52 |
| - refresh: params.get('refresh'), |
53 |
| - expiresUtc: params.get('expiresUtc'), |
54 |
| - }; |
55 |
| - |
56 |
| - if (null === oAuth.bearer || null === oAuth.refresh || null === oAuth.expiresUtc) { |
57 |
| - this.onLoginFailed(); |
58 |
| - return; |
59 |
| - } |
60 |
| - |
61 |
| - this.oAuth = { |
62 |
| - bearer: oAuth.bearer, |
63 |
| - refresh: oAuth.refresh, |
64 |
| - expiresUtc: oAuth.expiresUtc |
65 |
| - }; |
66 |
| - |
67 |
| - // DO NOT assign this.desktopData until AFTER you update the clipboard successfully. |
68 |
| - // The absence of a value on this.desktopData is what tells the application we haven't logged in yet. |
69 |
| - const json = JSON.stringify(oAuth); |
70 |
| - navigator.clipboard.writeText(json); |
71 |
| - this.desktopData = json; |
72 |
| - |
73 |
| - this.loading = false; |
74 |
| - this.timer = interval(1000) |
75 |
| - .subscribe({ |
76 |
| - next: _ => { |
77 |
| - this.runCheckForLogin(); |
78 |
| - }, |
79 |
| - error: e => { |
80 |
| - console.error(e); |
81 |
| - } |
82 |
| - }); |
83 | 35 | },
|
84 | 36 | error: (_: HttpErrorResponse) => {
|
85 | 37 | this.onLoginFailed();
|
86 | 38 | }
|
87 | 39 | });
|
88 | 40 | }
|
89 | 41 |
|
90 |
| - private runCheckForLogin() { |
91 |
| - try { |
92 |
| - navigator.clipboard.readText().then(text => { |
93 |
| - // If the text matches what we put on the clipboard then we aren't signed in yet. |
94 |
| - if (undefined === this.desktopData || text === this.desktopData) { |
95 |
| - return; |
96 |
| - } |
97 |
| - |
98 |
| - // If the text does match, we are signed in on the desktop app. Maybe...kinda....might be...you never know... |
99 |
| - // Don't judge me. -.- |
100 |
| - this.timer?.unsubscribe(); |
101 |
| - this.loggedIn = true; |
102 |
| - }) |
103 |
| - } catch { |
104 |
| - // Do nothing, just don't crash. |
105 |
| - } |
106 |
| - } |
107 |
| - |
108 |
| - onLoginFailed(message = ':( Failed to login, please try again'): void { |
| 42 | + onLoginFailed(message = 'Internal error logging you in'): void { |
109 | 43 | this.error = message;
|
110 |
| - this.loading = false; |
111 | 44 | }
|
112 |
| - |
113 |
| - protected readonly JSON = JSON; |
114 |
| - protected readonly window = window; |
115 | 45 | }
|
0 commit comments