]>
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 | ||
40360c17 K |
62 | onTermsClick (event: Event, instanceInformation: HTMLElement) { |
63 | event.preventDefault() | |
64 | ||
65 | if (this.accordion) { | |
66 | this.accordion.expand('terms') | |
67 | instanceInformation.scrollIntoView({ behavior: 'smooth' }) | |
68 | } | |
69 | } | |
70 | ||
3b3b1820 | 71 | isEmailDisabled () { |
ba430d75 | 72 | return this.serverConfig.email.enabled === false |
3b3b1820 C |
73 | } |
74 | ||
df98563e | 75 | ngOnInit () { |
4a8d113b C |
76 | const snapshot = this.route.snapshot |
77 | ||
2345e138 C |
78 | // Avoid undefined errors when accessing form error properties |
79 | this.buildForm({ | |
80 | username: LOGIN_USERNAME_VALIDATOR, | |
81 | password: LOGIN_PASSWORD_VALIDATOR | |
82 | }) | |
83 | ||
4a8d113b C |
84 | this.serverConfig = snapshot.data.serverConfig |
85 | ||
86 | if (snapshot.queryParams.externalAuthToken) { | |
87 | this.loadExternalAuthToken(snapshot.queryParams.username, snapshot.queryParams.externalAuthToken) | |
88 | return | |
89 | } | |
ba430d75 | 90 | |
bc90883f C |
91 | if (snapshot.queryParams.externalAuthError) { |
92 | this.externalAuthError = true | |
93 | return | |
94 | } | |
cb28bb92 | 95 | |
13e7c3b0 C |
96 | const previousUrl = this.redirectService.getPreviousUrl() |
97 | if (previousUrl && previousUrl !== '/') { | |
98 | this.storage.setItem(LoginComponent.SESSION_STORAGE_REDIRECT_URL_KEY, previousUrl) | |
99 | } | |
ebefc902 C |
100 | } |
101 | ||
102 | ngAfterViewInit () { | |
f375bb3d | 103 | this.hooks.runAction('action:login.init', 'login') |
ebefc902 C |
104 | } |
105 | ||
106 | getExternalLogins () { | |
107 | return this.serverConfig.plugin.registeredExternalAuths | |
108 | } | |
9fe44067 | 109 | |
ebefc902 | 110 | getAuthHref (auth: RegisteredExternalAuthConfig) { |
0bc53e20 | 111 | return PluginsManager.getExternalAuthHref(auth) |
0f6da32b C |
112 | } |
113 | ||
df98563e C |
114 | login () { |
115 | this.error = null | |
4b2f33f3 | 116 | |
df98563e | 117 | const { username, password } = this.form.value |
4b2f33f3 | 118 | |
2b084d70 | 119 | this.authService.login(username, password) |
1378c0d3 C |
120 | .subscribe({ |
121 | next: () => this.redirectService.redirectToPreviousRoute(), | |
192ea60b | 122 | |
1378c0d3 C |
123 | error: err => this.handleError(err) |
124 | }) | |
b1794c53 | 125 | } |
ecb4e35f C |
126 | |
127 | askResetPassword () { | |
128 | this.userService.askResetPassword(this.forgotPasswordEmail) | |
1378c0d3 C |
129 | .subscribe({ |
130 | next: () => { | |
66357162 C |
131 | const message = $localize`An email with the reset password instructions will be sent to ${this.forgotPasswordEmail}. |
132 | The link will expire within 1 hour.` | |
133 | ||
f8b2c1b4 | 134 | this.notifier.success(message) |
ecb4e35f C |
135 | this.hideForgotPasswordModal() |
136 | }, | |
137 | ||
1378c0d3 C |
138 | error: err => this.notifier.error(err.message) |
139 | }) | |
ecb4e35f C |
140 | } |
141 | ||
ecb4e35f | 142 | openForgotPasswordModal () { |
63347a0f | 143 | this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal) |
ecb4e35f C |
144 | } |
145 | ||
146 | hideForgotPasswordModal () { | |
63347a0f | 147 | this.openedForgotPasswordModal.close() |
ecb4e35f | 148 | } |
4a8d113b | 149 | |
40360c17 K |
150 | onInstanceAboutAccordionInit (instanceAboutAccordion: InstanceAboutAccordionComponent) { |
151 | this.accordion = instanceAboutAccordion.accordion | |
152 | } | |
153 | ||
7f28f2dd C |
154 | hasUsernameUppercase () { |
155 | return this.form.value['username'].match(/[A-Z]/) | |
156 | } | |
157 | ||
4a8d113b C |
158 | private loadExternalAuthToken (username: string, token: string) { |
159 | this.isAuthenticatedWithExternalAuth = true | |
160 | ||
161 | this.authService.login(username, null, token) | |
1378c0d3 | 162 | .subscribe({ |
cb28bb92 | 163 | next: () => { |
13e7c3b0 C |
164 | const redirectUrl = this.storage.getItem(LoginComponent.SESSION_STORAGE_REDIRECT_URL_KEY) |
165 | if (redirectUrl) { | |
166 | this.storage.removeItem(LoginComponent.SESSION_STORAGE_REDIRECT_URL_KEY) | |
167 | return this.router.navigateByUrl(redirectUrl) | |
168 | } | |
169 | ||
170 | this.redirectService.redirectToLatestSessionRoute() | |
cb28bb92 | 171 | }, |
1378c0d3 C |
172 | |
173 | error: err => { | |
174 | this.handleError(err) | |
175 | this.isAuthenticatedWithExternalAuth = false | |
176 | } | |
177 | }) | |
4a8d113b C |
178 | } |
179 | ||
180 | private handleError (err: any) { | |
66357162 C |
181 | if (err.message.indexOf('credentials are invalid') !== -1) this.error = $localize`Incorrect username or password.` |
182 | else if (err.message.indexOf('blocked') !== -1) this.error = $localize`Your account is blocked.` | |
4a8d113b C |
183 | else this.error = err.message |
184 | } | |
b1794c53 | 185 | } |