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