UNPKG

ozone-login-form

Version:

generic login form for ozone authentication

212 lines (211 loc) 7.39 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; 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> * * ``` */ let OzoneLoginForm = class OzoneLoginForm extends Polymer.Element { /** * ### 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> * * ``` */ constructor() { super(...arguments); this.username = ''; this.password = ''; this.errorMessage = ''; this.messageOnEmpty = 'The username and/or password is empty'; this.messageOnInvalid = 'Invalid username and/or password'; this.messageOnUnknown = 'Unknown error. Please check your connection'; this.alignButtons = 'center'; this.usernameLabel = 'Username'; this.passwordLabel = 'Password'; this.submitLabel = 'Login'; this.lostPasswordLabel = 'Lost password'; this.showLostPassword = true; this.registerLabel = 'Register'; this.showRegister = true; this.api = getOzoneApiAuthentication(); } register(e) { this.dispatchEvent(new CustomEvent('register-clicked', {})); } lostPassword(e) { this.dispatchEvent(new CustomEvent('password-clicked', { detail: { username: this.username } })); } submitForm(e) { if (this.dispatchEvent(new CustomEvent('before-submit', { detail: { form: this.$.form }, cancelable: true }))) { this.validateForm(); this.api.ozoneConnect(this.username, this.password) .then((res) => { //console.log('is success') this.dispatchEvent(new CustomEvent('auth-success', { detail: { success: true } })); this.set('errorMessage', ''); }) .catch((err) => { 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 } })); }); } } validateForm() { this.$.username.validate(); this.$.password.validate(); if (!this.username.trim().length) return this.$.username.focus(); if (!this.password.trim().length) this.$.password.focus(); } onKeyPress(e) { if (e.key === 'Enter') { this.validateForm(); if (this.$.username.value.trim().length && this.$.password.value.trim().length) { this.submitForm(e); } } } }; __decorate([ property({ type: String }), __metadata("design:type", String) ], OzoneLoginForm.prototype, "username", void 0); __decorate([ property({ type: String }), __metadata("design:type", String) ], OzoneLoginForm.prototype, "password", void 0); __decorate([ property({ type: String }), __metadata("design:type", String) ], OzoneLoginForm.prototype, "errorMessage", void 0); __decorate([ property({ type: String }), __metadata("design:type", String) ], OzoneLoginForm.prototype, "messageOnEmpty", void 0); __decorate([ property({ type: String }), __metadata("design:type", String) ], OzoneLoginForm.prototype, "messageOnInvalid", void 0); __decorate([ property({ type: String }), __metadata("design:type", String) ], OzoneLoginForm.prototype, "messageOnUnknown", void 0); __decorate([ property({ type: String, notify: true }), __metadata("design:type", String) ], OzoneLoginForm.prototype, "alignButtons", void 0); __decorate([ property({ type: String }), __metadata("design:type", String) ], OzoneLoginForm.prototype, "usernameLabel", void 0); __decorate([ property({ type: String }), __metadata("design:type", String) ], OzoneLoginForm.prototype, "passwordLabel", void 0); __decorate([ property({ type: String }), __metadata("design:type", String) ], OzoneLoginForm.prototype, "submitLabel", void 0); __decorate([ property({ type: String }), __metadata("design:type", String) ], OzoneLoginForm.prototype, "lostPasswordLabel", void 0); __decorate([ property({ type: Boolean }), __metadata("design:type", Boolean) ], OzoneLoginForm.prototype, "showLostPassword", void 0); __decorate([ property({ type: String }), __metadata("design:type", String) ], OzoneLoginForm.prototype, "registerLabel", void 0); __decorate([ property({ type: Boolean }), __metadata("design:type", Boolean) ], OzoneLoginForm.prototype, "showRegister", void 0); __decorate([ property({ type: Object }), __metadata("design:type", OzoneApiAuthentication) ], OzoneLoginForm.prototype, "api", void 0); OzoneLoginForm = __decorate([ customElement('ozone-login-form') ], OzoneLoginForm); export { OzoneLoginForm };