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