ozone-login-form
Version:
generic login form for ozone authentication
147 lines (118 loc) • 3.77 kB
text/typescript
import 'polymer/polymer.html'
import 'paper-input/paper-input.html'
import 'paper-button/paper-button.html'
import {customElement, property} from 'taktik-polymer-typescript'
import {OzoneApiAuthentication, getOzoneApiAuthentication} from 'ozone-api-authentication'
import './ozone-login-form.html'
/**
* ### ozone-login-form
*
* A generic login form for Ozone.
* It uses 'ozone-authentication-api' to automatically authenticate to Ozone.
*
*
* ### Usage
*
* ```javascript
* import 'ozone-login-form'
*
* ...
*
* <ozone-login-form
* on-register-clicked="..."
* on-password-clicked="..."
* align-buttons="right|center|left"
* username-label="..."
* password-label="..."
* register-label="..."
* lost-password-label="..."
* submit-label="..."
* message-on-empty="..."
* message-on-invalid="..."
* message-on-unknown="..."
* >
* </ozone-login-form>
*
* ```
*/
export class OzoneLoginForm extends Polymer.Element {
public username: string = ''
public password: string = ''
public errorMessage: string = ''
public messageOnEmpty: string = 'The username and/or password is empty'
public messageOnInvalid: string = 'Invalid username and/or password'
public messageOnUnknown: string = 'Unknown error. Please check your connection'
public alignButtons: string = 'center'
public usernameLabel: string = 'Username'
public passwordLabel: string = 'Password'
public submitLabel: string = 'Login'
public lostPasswordLabel: string = 'Lost password'
public showLostPassword: boolean = true
public registerLabel: string = 'Register'
public showRegister: boolean = true
private api: OzoneApiAuthentication = getOzoneApiAuthentication()
public register(e: Event): void {
this.dispatchEvent(new CustomEvent('register-clicked', {}))
}
public lostPassword(e: Event): void {
this.dispatchEvent(new CustomEvent('password-clicked', {detail: {username: this.username}}))
}
public submitForm(e: Event): void {
if (this.dispatchEvent(new CustomEvent('before-submit', {detail: {form: this.$.form}, cancelable: true }))){
this.validateForm()
this.api.ozoneConnect(this.username, this.password)
.then((res: XMLHttpRequest) => {
//console.log('is success')
this.dispatchEvent(new CustomEvent('auth-success', {detail: {success: true}}))
this.set('errorMessage', '')
})
.catch((err: XMLHttpRequest) => {
if (err.status === 400){
this.set('errorMessage', this.messageOnEmpty)
}
else if (err.status === 403) {
this.set('errorMessage', this.messageOnInvalid)
}
else {
this.set('errorMessage', this.messageOnUnknown)
}
this.dispatchEvent(new CustomEvent('auth-error', {
detail: {
status: err.status,
message: this.errorMessage
}
}))
})
}
}
private validateForm(): void {
this.$.username.validate()
this.$.password.validate()
if (!this.username.trim().length) return this.$.username.focus()
if (!this.password.trim().length) this.$.password.focus()
}
private onKeyPress(e: KeyboardEvent): void {
if (e.key === 'Enter'){
this.validateForm()
if (this.$.username.value.trim().length && this.$.password.value.trim().length){
this.submitForm(e)
}
}
}
}