]>
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' |
67ed6552 C |
6 | import { FormReactive, FormValidatorService, LoginValidatorsService } from '@app/shared/shared-forms' |
7 | import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap' | |
8 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
9 | import { RegisteredExternalAuthConfig, ServerConfig } from '@shared/models' | |
b1794c53 C |
10 | |
11 | @Component({ | |
a840d396 | 12 | selector: 'my-login', |
d235f6b0 C |
13 | templateUrl: './login.component.html', |
14 | styleUrls: [ './login.component.scss' ] | |
b1794c53 C |
15 | }) |
16 | ||
ebefc902 C |
17 | export class LoginComponent extends FormReactive implements OnInit, AfterViewInit { |
18 | @ViewChild('usernameInput', { static: false }) usernameInput: ElementRef | |
f36da21e | 19 | @ViewChild('forgotPasswordModal', { static: true }) forgotPasswordModal: ElementRef |
ecb4e35f | 20 | |
df98563e | 21 | error: string = null |
ecb4e35f | 22 | forgotPasswordEmail = '' |
bc90883f | 23 | |
4a8d113b | 24 | isAuthenticatedWithExternalAuth = false |
bc90883f | 25 | externalAuthError = false |
ebefc902 | 26 | externalLogins: string[] = [] |
192ea60b | 27 | |
63347a0f | 28 | private openedForgotPasswordModal: NgbModalRef |
ba430d75 | 29 | private serverConfig: ServerConfig |
63347a0f | 30 | |
b1d40cff | 31 | constructor ( |
d18d6478 | 32 | protected formValidatorService: FormValidatorService, |
ba430d75 | 33 | private route: ActivatedRoute, |
63347a0f | 34 | private modalService: NgbModal, |
e309822b | 35 | private loginValidatorsService: LoginValidatorsService, |
b1d40cff C |
36 | private authService: AuthService, |
37 | private userService: UserService, | |
b1d40cff | 38 | private redirectService: RedirectService, |
f8b2c1b4 | 39 | private notifier: Notifier, |
f375bb3d | 40 | private hooks: HooksService, |
b1d40cff C |
41 | private i18n: I18n |
42 | ) { | |
df98563e | 43 | super() |
4b2f33f3 | 44 | } |
b1794c53 | 45 | |
2b084d70 | 46 | get signupAllowed () { |
ba430d75 | 47 | return this.serverConfig.signup.allowed === true |
2b084d70 C |
48 | } |
49 | ||
3b3b1820 | 50 | isEmailDisabled () { |
ba430d75 | 51 | return this.serverConfig.email.enabled === false |
3b3b1820 C |
52 | } |
53 | ||
df98563e | 54 | ngOnInit () { |
4a8d113b C |
55 | const snapshot = this.route.snapshot |
56 | ||
57 | this.serverConfig = snapshot.data.serverConfig | |
58 | ||
59 | if (snapshot.queryParams.externalAuthToken) { | |
60 | this.loadExternalAuthToken(snapshot.queryParams.username, snapshot.queryParams.externalAuthToken) | |
61 | return | |
62 | } | |
ba430d75 | 63 | |
bc90883f C |
64 | if (snapshot.queryParams.externalAuthError) { |
65 | this.externalAuthError = true | |
66 | return | |
67 | } | |
68 | ||
d18d6478 | 69 | this.buildForm({ |
e309822b C |
70 | username: this.loginValidatorsService.LOGIN_USERNAME, |
71 | password: this.loginValidatorsService.LOGIN_PASSWORD | |
d18d6478 | 72 | }) |
ebefc902 C |
73 | } |
74 | ||
75 | ngAfterViewInit () { | |
76 | if (this.usernameInput) { | |
77 | this.usernameInput.nativeElement.focus() | |
78 | } | |
f375bb3d C |
79 | |
80 | this.hooks.runAction('action:login.init', 'login') | |
ebefc902 C |
81 | } |
82 | ||
83 | getExternalLogins () { | |
84 | return this.serverConfig.plugin.registeredExternalAuths | |
85 | } | |
9fe44067 | 86 | |
ebefc902 C |
87 | getAuthHref (auth: RegisteredExternalAuthConfig) { |
88 | return environment.apiUrl + `/plugins/${auth.name}/${auth.version}/auth/${auth.authName}` | |
0f6da32b C |
89 | } |
90 | ||
df98563e C |
91 | login () { |
92 | this.error = null | |
4b2f33f3 | 93 | |
df98563e | 94 | const { username, password } = this.form.value |
4b2f33f3 | 95 | |
2b084d70 C |
96 | this.authService.login(username, password) |
97 | .subscribe( | |
dae5ca24 | 98 | () => this.redirectService.redirectToPreviousRoute(), |
192ea60b | 99 | |
4a8d113b | 100 | err => this.handleError(err) |
2b084d70 | 101 | ) |
b1794c53 | 102 | } |
ecb4e35f C |
103 | |
104 | askResetPassword () { | |
105 | this.userService.askResetPassword(this.forgotPasswordEmail) | |
106 | .subscribe( | |
141b177d | 107 | () => { |
b1d40cff | 108 | const message = this.i18n( |
f88ee4a9 | 109 | 'An email with the reset password instructions will be sent to {{email}}. The link will expire within 1 hour.', |
b1d40cff C |
110 | { email: this.forgotPasswordEmail } |
111 | ) | |
f8b2c1b4 | 112 | this.notifier.success(message) |
ecb4e35f C |
113 | this.hideForgotPasswordModal() |
114 | }, | |
115 | ||
f8b2c1b4 | 116 | err => this.notifier.error(err.message) |
ecb4e35f C |
117 | ) |
118 | } | |
119 | ||
ecb4e35f | 120 | openForgotPasswordModal () { |
63347a0f | 121 | this.openedForgotPasswordModal = this.modalService.open(this.forgotPasswordModal) |
ecb4e35f C |
122 | } |
123 | ||
124 | hideForgotPasswordModal () { | |
63347a0f | 125 | this.openedForgotPasswordModal.close() |
ecb4e35f | 126 | } |
4a8d113b C |
127 | |
128 | private loadExternalAuthToken (username: string, token: string) { | |
129 | this.isAuthenticatedWithExternalAuth = true | |
130 | ||
131 | this.authService.login(username, null, token) | |
132 | .subscribe( | |
133 | () => this.redirectService.redirectToPreviousRoute(), | |
134 | ||
135 | err => { | |
136 | this.handleError(err) | |
137 | this.isAuthenticatedWithExternalAuth = false | |
138 | } | |
139 | ) | |
140 | } | |
141 | ||
142 | private handleError (err: any) { | |
143 | if (err.message.indexOf('credentials are invalid') !== -1) this.error = this.i18n('Incorrect username or password.') | |
144 | else if (err.message.indexOf('blocked') !== -1) this.error = this.i18n('You account is blocked.') | |
145 | else this.error = err.message | |
146 | } | |
b1794c53 | 147 | } |