]>
Commit | Line | Data |
---|---|---|
ebefc902 | 1 | import { environment } from 'src/environments/environment' |
67ed6552 C |
2 | import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core' |
3 | import { ActivatedRoute } from '@angular/router' | |
4 | import { AuthService, Notifier, RedirectService, UserService } from '@app/core' | |
f375bb3d | 5 | import { HooksService } from '@app/core/plugins/hooks.service' |
7ed1edbb C |
6 | import { LOGIN_PASSWORD_VALIDATOR, LOGIN_USERNAME_VALIDATOR } from '@app/shared/form-validators/login-validators' |
7 | import { FormReactive, FormValidatorService } from '@app/shared/shared-forms' | |
a3664dfd | 8 | import { InstanceAboutAccordionComponent } from '@app/shared/shared-instance' |
40360c17 | 9 | import { NgbAccordion, NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap' |
67ed6552 | 10 | import { RegisteredExternalAuthConfig, ServerConfig } from '@shared/models' |
b1794c53 C |
11 | |
12 | @Component({ | |
a840d396 | 13 | selector: 'my-login', |
d235f6b0 C |
14 | templateUrl: './login.component.html', |
15 | styleUrls: [ './login.component.scss' ] | |
b1794c53 C |
16 | }) |
17 | ||
ebefc902 | 18 | export class LoginComponent extends FormReactive implements OnInit, AfterViewInit { |
f36da21e | 19 | @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef |
ecb4e35f | 20 | |
40360c17 | 21 | accordion: NgbAccordion |
df98563e | 22 | error: string = null |
ecb4e35f | 23 | forgotPasswordEmail = '' |
bc90883f | 24 | |
4a8d113b | 25 | isAuthenticatedWithExternalAuth = false |
bc90883f | 26 | externalAuthError = false |
ebefc902 | 27 | externalLogins: string[] = [] |
192ea60b | 28 | |
40360c17 K |
29 | instanceInformationPanels = { |
30 | terms: true, | |
31 | administrators: false, | |
32 | features: false, | |
33 | moderation: false, | |
34 | codeOfConduct: false | |
35 | } | |
36 | ||
63347a0f | 37 | private openedForgotPasswordModal: NgbModalRef |
ba430d75 | 38 | private serverConfig: ServerConfig |
63347a0f | 39 | |
b1d40cff | 40 | constructor ( |
d18d6478 | 41 | protected formValidatorService: FormValidatorService, |
ba430d75 | 42 | private route: ActivatedRoute, |
63347a0f | 43 | private modalService: NgbModal, |
b1d40cff C |
44 | private authService: AuthService, |
45 | private userService: UserService, | |
b1d40cff | 46 | private redirectService: RedirectService, |
f8b2c1b4 | 47 | private notifier: Notifier, |
66357162 | 48 | private hooks: HooksService |
9df52d66 | 49 | ) { |
df98563e | 50 | super() |
4b2f33f3 | 51 | } |
b1794c53 | 52 | |
2b084d70 | 53 | get signupAllowed () { |
ba430d75 | 54 | return this.serverConfig.signup.allowed === true |
2b084d70 C |
55 | } |
56 | ||
40360c17 K |
57 | onTermsClick (event: Event, instanceInformation: HTMLElement) { |
58 | event.preventDefault() | |
59 | ||
60 | if (this.accordion) { | |
61 | this.accordion.expand('terms') | |
62 | instanceInformation.scrollIntoView({ behavior: 'smooth' }) | |
63 | } | |
64 | } | |
65 | ||
3b3b1820 | 66 | isEmailDisabled () { |
ba430d75 | 67 | return this.serverConfig.email.enabled === false |
3b3b1820 C |
68 | } |
69 | ||
df98563e | 70 | ngOnInit () { |
4a8d113b C |
71 | const snapshot = this.route.snapshot |
72 | ||
2345e138 C |
73 | // Avoid undefined errors when accessing form error properties |
74 | this.buildForm({ | |
75 | username: LOGIN_USERNAME_VALIDATOR, | |
76 | password: LOGIN_PASSWORD_VALIDATOR | |
77 | }) | |
78 | ||
4a8d113b C |
79 | this.serverConfig = snapshot.data.serverConfig |
80 | ||
81 | if (snapshot.queryParams.externalAuthToken) { | |
82 | this.loadExternalAuthToken(snapshot.queryParams.username, snapshot.queryParams.externalAuthToken) | |
83 | return | |
84 | } | |
ba430d75 | 85 | |
bc90883f C |
86 | if (snapshot.queryParams.externalAuthError) { |
87 | this.externalAuthError = true | |
88 | return | |
89 | } | |
ebefc902 C |
90 | } |
91 | ||
92 | ngAfterViewInit () { | |
f375bb3d | 93 | this.hooks.runAction('action:login.init', 'login') |
ebefc902 C |
94 | } |
95 | ||
96 | getExternalLogins () { | |
97 | return this.serverConfig.plugin.registeredExternalAuths | |
98 | } | |
9fe44067 | 99 | |
ebefc902 C |
100 | getAuthHref (auth: RegisteredExternalAuthConfig) { |
101 | return environment.apiUrl + `/plugins/${auth.name}/${auth.version}/auth/${auth.authName}` | |
0f6da32b C |
102 | } |
103 | ||
df98563e C |
104 | login () { |
105 | this.error = null | |
4b2f33f3 | 106 | |
df98563e | 107 | const { username, password } = this.form.value |
4b2f33f3 | 108 | |
2b084d70 | 109 | this.authService.login(username, password) |
1378c0d3 C |
110 | .subscribe({ |
111 | next: () => this.redirectService.redirectToPreviousRoute(), | |
192ea60b | 112 | |
1378c0d3 C |
113 | error: err => this.handleError(err) |
114 | }) | |
b1794c53 | 115 | } |
ecb4e35f C |
116 | |
117 | askResetPassword () { | |
118 | this.userService.askResetPassword(this.forgotPasswordEmail) | |
1378c0d3 C |
119 | .subscribe({ |
120 | next: () => { | |
66357162 C |
121 | const message = $localize`An email with the reset password instructions will be sent to ${this.forgotPasswordEmail}. |
122 | The link will expire within 1 hour.` | |
123 | ||
f8b2c1b4 | 124 | this.notifier.success(message) |
ecb4e35f C |
125 | this.hideForgotPasswordModal() |
126 | }, | |
127 | ||
1378c0d3 C |
128 | error: err => this.notifier.error(err.message) |
129 | }) | |
ecb4e35f C |
130 | } |
131 | ||
ecb4e35f | 132 | openForgotPasswordModal () { |
63347a0f | 133 | this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal) |
ecb4e35f C |
134 | } |
135 | ||
136 | hideForgotPasswordModal () { | |
63347a0f | 137 | this.openedForgotPasswordModal.close() |
ecb4e35f | 138 | } |
4a8d113b | 139 | |
40360c17 K |
140 | onInstanceAboutAccordionInit (instanceAboutAccordion: InstanceAboutAccordionComponent) { |
141 | this.accordion = instanceAboutAccordion.accordion | |
142 | } | |
143 | ||
7f28f2dd C |
144 | hasUsernameUppercase () { |
145 | return this.form.value['username'].match(/[A-Z]/) | |
146 | } | |
147 | ||
4a8d113b C |
148 | private loadExternalAuthToken (username: string, token: string) { |
149 | this.isAuthenticatedWithExternalAuth = true | |
150 | ||
151 | this.authService.login(username, null, token) | |
1378c0d3 C |
152 | .subscribe({ |
153 | next: () => this.redirectService.redirectToPreviousRoute(), | |
154 | ||
155 | error: err => { | |
156 | this.handleError(err) | |
157 | this.isAuthenticatedWithExternalAuth = false | |
158 | } | |
159 | }) | |
4a8d113b C |
160 | } |
161 | ||
162 | private handleError (err: any) { | |
66357162 C |
163 | if (err.message.indexOf('credentials are invalid') !== -1) this.error = $localize`Incorrect username or password.` |
164 | else if (err.message.indexOf('blocked') !== -1) this.error = $localize`Your account is blocked.` | |
4a8d113b C |
165 | else this.error = err.message |
166 | } | |
b1794c53 | 167 | } |