Advertisement
Guest User

Untitled

a guest
Jan 12th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1. <template>
  2. <div class="container">
  3. <h3>Jogo da Memória</h3>
  4. <br>
  5. <div class="text">
  6.  
  7. Login:
  8. <input v-model="email" >
  9. <br>
  10. <br>
  11. Palavra-passe:
  12. <input type="password" v-model="password" >
  13.  
  14. <br>
  15. <br>
  16. <p><button v-on:click="login" class="btn btn-default" >Confirmar</button> </p>
  17.  
  18.  
  19. </div>
  20. </div>
  21. </template>
  22.  
  23. <script>
  24. export default {
  25. data: function(){
  26.  
  27. return {
  28. username: "",
  29. password: "",
  30. email: "",
  31. changed: true
  32.  
  33. }
  34.  
  35. },
  36. methods:{
  37. login: function () {
  38. if((this.email.includes('@'))){
  39. axios.post('api/users/findnickname',{
  40. email: this.email
  41. })
  42. .then(response=> {
  43. console.log(this.username);
  44. this.username = response.data;
  45. console.log("USERNAME atribuido: "+this.username);
  46.  
  47. axios.post('/api/login', {
  48. email: this.email,
  49. password: this.password
  50. })
  51. .then(response => {
  52. this.$store.state.token= response.data.access_token;
  53. this.$store.state.isUserLogged = true;
  54. this.$emit('tokenAssign');
  55. this.$store.state.username = this.username;
  56. //if() SE FOR ADMIN MANDAR PARA OUTRO SITIO
  57. axios.post('api/users/isAdmin',{
  58. username: this.username
  59. }).then(response=> {
  60. if(response.data===1){
  61. console.log("ADMIN durante o login, a definir state.isAdmin..");
  62. this.$store.state.isAdmin=1;
  63. this.$router.push('/admin');//TEMPORARIO
  64. }else{
  65. this.$router.push('/game');//TEMPORARIO
  66. }
  67.  
  68. });
  69. // console.log(this.store().username)
  70. })
  71. .catch(function (error) {
  72. alert(error.response.data);
  73. console.log(error);
  74. });
  75.  
  76. }).catch(function (error) {
  77. alert(error.response.data);
  78. console.log(error);
  79. });
  80.  
  81.  
  82.  
  83.  
  84. }else{
  85. let usernameOld= this.email;
  86. axios.post('api/users/findemail',{
  87. username: this.email //Campo onde o user preencheu com username vai ser enviado para a api para retornar o email
  88. })
  89.  
  90. .then(response=> {
  91.  
  92. this.email = response.data; //o username vai ser substituido pelo email para facilitar o login da api
  93.  
  94. axios.post('/api/login', {
  95. email: this.email,
  96. password: this.password
  97. })
  98. .then(response => {
  99. this.$store.state.token= response.data.access_token;
  100. this.$store.state.isUserLogged = true;
  101. localStorage.setItem('cookie', response.data.access_token);
  102. this.$emit('tokenAssign');
  103. this.$store.state.username = usernameOld;
  104.  
  105. //if() SE FOR ADMIN MANDAR PARA OUTRO SITIO
  106. axios.post('api/users/isAdmin',{
  107. username: usernameOld
  108. }).then(response=> {
  109. if(response.data===1){
  110. console.log("ADMIN durante o login, a definir state.isAdmin..");
  111. this.$store.state.isAdmin=1;
  112. this.$router.push('/admin');//TEMPORARIO
  113. }else{
  114. this.$router.push('/game');//TEMPORARIO
  115. }
  116.  
  117. });
  118.  
  119. //this.$router.push('/game');
  120.  
  121. })
  122. .catch(function (error) {
  123. console.log(error);
  124. });
  125. });
  126.  
  127.  
  128. }
  129.  
  130.  
  131.  
  132.  
  133. },
  134. },
  135. mounted() {
  136. console.log('Component mounted.')
  137. }
  138. }
  139. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement