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