Advertisement
Guest User

Untitled

a guest
Jan 11th, 2019
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <template>
  2.     <v-container fluid fill-height>
  3.         <v-snackbar
  4.             v-model="snackbar"
  5.             :bottom="true"
  6.             :center="true"
  7.             :timeout="2000"
  8.             color="error"
  9.         >{{ text }}</v-snackbar>
  10.  
  11.         <v-layout align-center justify-center>
  12.             <v-flex xs12 sm8 md4>
  13.                 <v-card class="pa-3">
  14.                     <v-card-title>
  15.                         <span class="headline">Masuk</span>
  16.                     </v-card-title>
  17.                     <v-card-text>
  18.                         <v-container grid-list-md>
  19.                             <v-layout row wrap>
  20.                                 <v-flex xs12>
  21.                                     <v-text-field
  22.                                         type="email"
  23.                                         label="E-mail"
  24.                                         v-model="user.email"
  25.                                         :error-messages="emailErrors"
  26.                                         @input="$v.user.email.$touch()"
  27.                                         @blur="$v.user.email.$touch()"
  28.                                     ></v-text-field>
  29.                                 </v-flex>
  30.                                 <v-flex xs12>
  31.                                     <v-text-field
  32.                                         label="Sandi"
  33.                                         v-model="user.password"
  34.                                         :error-messages="passwordErrors"
  35.                                         :append-icon="pass ? 'visibility' : 'visibility_off'"
  36.                                         :append-icon-cb="changeVisibility"
  37.                                         :type="pass ? 'password' : 'text'"
  38.                                         @input="$v.user.password.$touch()"
  39.                                         @blur="$v.user.password.$touch()"
  40.                                     ></v-text-field>
  41.                                 </v-flex>
  42.                             </v-layout>
  43.                         </v-container>
  44.                     </v-card-text>
  45.                     <v-card-actions>
  46.                         <v-menu>
  47.                             <v-btn flat small slot="activator">
  48.                                 Lainnya <v-icon>arrow_drop_down</v-icon>
  49.                             </v-btn>
  50.                             <v-list>
  51.                                 <v-list-tile to="/pass-reset">
  52.                                     <v-list-tile-title>Lupa Sandi</v-list-tile-title>
  53.                                 </v-list-tile>
  54.                                 <v-list-tile to="/resend">
  55.                                     <v-list-tile-title>Kirim Ulang Verifikasi</v-list-tile-title>
  56.                                 </v-list-tile>
  57.                                 <v-list-tile to="/register">
  58.                                     <v-list-tile-title>Daftar</v-list-tile-title>
  59.                                 </v-list-tile>
  60.                             </v-list>
  61.                         </v-menu>
  62.                         <v-spacer></v-spacer>
  63.                         <v-btn
  64.                             color="black"
  65.                             class="white--text"
  66.                             @click="userSignin"
  67.                             :disabled="!$v.user.$dirty || $v.user.$error || loading"
  68.                         >Berikutnya</v-btn>
  69.                     </v-card-actions>
  70.                 </v-card>
  71.             </v-flex>
  72.         </v-layout>
  73.         <v-btn
  74.             dark
  75.             fixed
  76.             bottom
  77.             right
  78.             round
  79.             to="/"
  80.         >
  81.             <v-icon left>mdi-home-circle</v-icon> Beranda
  82.         </v-btn>
  83.     </v-container>
  84. </template>
  85.  
  86. <script>
  87.     import { required, email, minLength } from 'vuelidate/lib/validators'
  88.     import store from '../store'
  89.  
  90.     export default {
  91.         metaInfo: {
  92.             titleTemplate: 'Masuk'
  93.         },
  94.  
  95.         data: () => ({
  96.             user: {
  97.                 email: '',
  98.                 password: ''
  99.             },
  100.             snackbar: false,
  101.             pass: true,
  102.             text: null,
  103.             loading: false
  104.         }),
  105.        
  106.         validations: {
  107.             user: {
  108.                 email: { required, email },
  109.                 password: { required, minLength: minLength(8) }
  110.             }
  111.         },
  112.        
  113.         computed: {
  114.             emailErrors() {
  115.                 const errors = []
  116.                 if (!this.$v.user.email.$dirty) return errors
  117.                 !this.$v.user.email.required && errors.push('Email dibutuhkan')
  118.                 !this.$v.user.email.email && errors.push('Email tidak valid')
  119.                 return errors
  120.             },
  121.            
  122.             passwordErrors() {
  123.                 const errors = []
  124.                 if (!this.$v.user.password.$dirty) return errors
  125.                 !this.$v.user.password.required && errors.push('Sandi dibutuhkan')
  126.                 !this.$v.user.password.minLength && errors.push('Minimal 8 karakter')
  127.                 return errors
  128.             }
  129.         },
  130.        
  131.         methods: {
  132.             userSignin() {
  133.                 this.loading = true
  134.  
  135.                 const user = this.user 
  136.                 this.$auth.login({
  137.                     data: user,
  138.                     success: (res) => {
  139.                         localStorage.setItem('user', JSON.stringify(res.data.user))
  140.                         store.commit('loginUser')
  141.                         this.loading = false
  142.                     },
  143.                     error: (err) => {
  144.                         if (err.response.status == 400) {
  145.                             this.resetPass()
  146.                             this.snackbar = true
  147.                             this.text = 'Sandi salah'
  148.                         } else if (err.response.status == 401) {
  149.                             this.resetPass()
  150.                             this.snackbar = true
  151.                             this.text = 'E-mail belum diverifikasi'
  152.                         } else if (err.response.status == 403) {
  153.                             this.resetAll()
  154.                             this.snackbar = true
  155.                             this.text = 'Pengguna diblokir karena membuat kegiatan tidak sesuai'
  156.                         } else if (err.response.status == 404) {
  157.                             this.resetPass()
  158.                             this.snackbar = true
  159.                             this.text = 'E-mail tidak terdaftar'
  160.                         } else {
  161.                             this.snackbar = true
  162.                             this.text = 'Terjadi error. Coba kembali'
  163.                         }
  164.                         this.loading = false
  165.                     },
  166.                     rememberMe: true,
  167.                     redirect: this.$route.query.redirect ? this.$route.query.redirect : '/',
  168.                     fetchUser: true
  169.                 })
  170.             },
  171.  
  172.             changeVisibility() {
  173.                 this.pass = !this.pass
  174.             },
  175.  
  176.             resetPass() {
  177.                 this.user.password = ''
  178.                 this.$v.user.password.$reset()
  179.             },
  180.  
  181.             resetAll() {
  182.                 this.user = ''
  183.                 this.$v.user.$reset()
  184.             }
  185.         }
  186.     }
  187. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement