1+ <template >
2+ <div class =" container z-depth-1" >
3+ <div class =" row" >
4+ <h4 class =" center red-text" >{{ mode.toUpperCase() }}</h4 >
5+ <form @submit.prevent =" handleSubmit" class =" col s12" >
6+ <div class =" row" >
7+ <div class =" input-field col s12" >
8+ <i class =" material-icons prefix" >email</i >
9+ <input required v-model =" email" type =" email" class =" validate" />
10+ <label for =" icon_prefix" >Email Address</label >
11+ </div >
12+ </div >
13+ <div class =" row" >
14+ <div class =" input-field col s12" >
15+ <i class =" material-icons prefix" >security</i >
16+ <input minlength =" 6" v-model =" password" type =" password" class =" validate" />
17+ <label for =" icon_prefix" >Password</label >
18+ </div >
19+ </div >
20+ <div class =" row" >
21+ <div class =" input-field col s12" >
22+ <button class =" btn green waves-effect waves-light" type =" submit" >
23+ {{ submitted ? 'loading...' : mode }}
24+ <i class =" material-icons right" >send</i >
25+ </button >
26+ <button
27+ type =" button"
28+ style =" margin-left : 1em "
29+ class =" btn teal"
30+ @click =" mode = mode === 'login' ? 'register' : 'login'"
31+ >Switch to {{mode === 'login' ? 'register' : 'login'}}</button >
32+ </div >
33+ </div >
34+ </form >
35+ </div >
36+ </div >
37+ </template >
38+
39+ <script >
40+ import { firebaseApp } from " @/firebase" ;
41+ import M from " materialize-css/dist/js/materialize" ;
42+ export default {
43+ name: " AppAuthPage" ,
44+ data () {
45+ return {
46+ email: " " ,
47+ password: " " ,
48+ mode: " login" ,
49+ submitted: false ,
50+ };
51+ },
52+ methods: {
53+ handleSubmit : async function () {
54+ this .submitted = true ;
55+ if (! this .validateEmail (this .email )) {
56+ this .submitted = false ;
57+ return M .toast ({ html: " Please enter a valid Email Address" });
58+ }
59+ const err = await this .handleAuth ();
60+ if (err) {
61+ this .submitted = false ;
62+ return M .toast ({ html: err .message });
63+ }
64+ M .toast ({ html: ` You are Logged in as ${ this .email } ` });
65+ this .email = " " ;
66+ this .password = " " ;
67+ this .$router .push (" /" );
68+ },
69+ handleAuth : async function () {
70+ if (this .mode === " login" ) {
71+ try {
72+ await firebaseApp
73+ .auth ()
74+ .signInWithEmailAndPassword (this .email , this .password );
75+ } catch (e) {
76+ return e;
77+ }
78+ } else if (this .mode === " register" ) {
79+ try {
80+ await firebaseApp
81+ .auth ()
82+ .createUserWithEmailAndPassword (this .email , this .password );
83+ } catch (e) {
84+ return e;
85+ }
86+ }
87+ },
88+ validateEmail (email ) {
89+ const reg = new RegExp (
90+ / ^ (([^ <>()[\]\\ . ,;:\s @"] + (\. [^ <>()[\]\\ . ,;:\s @"] + )* )| (". + "))@((\[ [0-9 ] {1,3} \. [0-9 ] {1,3} \. [0-9 ] {1,3} \. [0-9 ] {1,3} \] )| (([a-zA-Z\-0 -9] + \. )+ [a-zA-Z ] {2,} ))$ /
91+ );
92+ return reg .test (email);
93+ },
94+ },
95+ };
96+ </script >
97+
98+ <style scoped>
99+ .container.z-depth-1 {
100+ padding : 10px 1em 0 1em ;
101+ margin-top : 3em ;
102+ }
103+ @media screen and (min-width : 1024px ) {
104+ .container.z-depth-1 {
105+ width : 60% ;
106+ }
107+ }
108+ </style >
0 commit comments