Login.js 1.9 KB

1
  1. import React from'react';import Module from'../Module';import Form from'../theme/Form.js';import User from'../../systems/services/User';import Modal from'../layout/Modal.js';import'./Login.css';let LoginFormInputs={object:{value:{Username:'',Password:''}}},RegisterFormInputs={object:{value:{Username:'',Password:'',"Confirm Password":'',Email:''}}};class UserLogin extends Module{constructor(a){super(a),this.state={username:'',password:''}}register(a){User.register({email:a.Email,username:a.Username,password:a.Password}).then(()=>{this.login(a)})}login(a){User.login({username:a.Username,password:a.Password}).then(()=>{this.user.set(User.me()),this.forceUpdate()})}logout(){User.logout().then(()=>{this.user.set(User.me()),this.forceUpdate()})}render(){let a=User.isLogged();return a?React.createElement('div',{className:'Logout'},React.createElement('span',null,'Logged in as ',User.getName(),'!'),React.createElement('button',{type:'button',className:'btn btn-secondary btn-sm small',onClick:()=>this.logout()},'Logout'),super.render()):React.createElement('div',{className:`Login`},React.createElement('button',{type:'button',className:'btn btn-secondary btn-sm',"data-toggle":'modal',"data-target":'#loginModal'},'Login'),React.createElement(Modal,{id:'loginModal',title:'Sign in'},this.state.register?[React.createElement(Form,{key:55,inputs:RegisterFormInputs,onSubmit:a=>this.register(a),buttonText:'Register'}),React.createElement('a',{className:'small',key:56,onClick:()=>this.setState({register:!1})},'Already a user ',React.createElement('br',null),' Click to here')]:[React.createElement(Form,{key:65,inputs:LoginFormInputs,onSubmit:a=>this.login(a),buttonText:'Login'}),React.createElement('a',{className:'small',key:66,onClick:()=>this.setState({register:!0})},'You don\'t have an account! ',React.createElement('br',null),' Register here')]),super.render())}}UserLogin.EditPosition='top-center',UserLogin.Outputs={user:{}};export default UserLogin;