Moved register and login requests to api.js.
This commit is contained in:
		
							
								
								
									
										39
									
								
								src/api.js
									
									
									
									
									
								
							
							
						
						
									
										39
									
								
								src/api.js
									
									
									
									
									
								
							| @@ -211,6 +211,43 @@ const getRequestStatus = (id, type, authorization_token=undefined) => { | |||||||
|     .catch(err => Promise.reject(err)) |     .catch(err => Promise.reject(err)) | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // - - - Seasoned user endpoints - - - | ||||||
|  |  | ||||||
|  | const register = (username, password) => { | ||||||
|  |   const url = new URL('v1/user', SEASONED_URL) | ||||||
|  |   const options = { | ||||||
|  |     method: 'POST', | ||||||
|  |     headers: { 'Content-Type': 'application/json' }, | ||||||
|  |     body: JSON.stringify({ username, password }) | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   return fetch(url.href, options) | ||||||
|  |     .then(resp => resp.json()) | ||||||
|  |     .catch(error => { | ||||||
|  |       console.error('Unexpected error occured before receiving response. Error:', error) | ||||||
|  |       // TODO log to sentry the issue here | ||||||
|  |       throw error | ||||||
|  |     }) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | const login = (username, password) => { | ||||||
|  |   const url = new URL('v1/user/login', SEASONED_URL) | ||||||
|  |   const options = { | ||||||
|  |     method: 'POST', | ||||||
|  |     headers: { 'Content-Type': 'application/json' }, | ||||||
|  |     body: JSON.stringify({ username, password }) | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   return fetch(url.href, options) | ||||||
|  |     .then(resp => checkStatus) | ||||||
|  |     .then(resp => resp.json()) | ||||||
|  |     .catch(error => { | ||||||
|  |       console.error('Unexpected error occured before receiving response. Error:', error) | ||||||
|  |       // TODO log to sentry the issue here | ||||||
|  |       throw error | ||||||
|  |     }) | ||||||
|  | } | ||||||
|  |  | ||||||
| // - - - Authenticate with plex - - - | // - - - Authenticate with plex - - - | ||||||
|  |  | ||||||
| const plexAuthenticate = (username, password) => { | const plexAuthenticate = (username, password) => { | ||||||
| @@ -303,6 +340,8 @@ export { | |||||||
|   request, |   request, | ||||||
|   getRequestStatus, |   getRequestStatus, | ||||||
|   plexAuthenticate, |   plexAuthenticate, | ||||||
|  |   register, | ||||||
|  |   login, | ||||||
|   getEmoji, |   getEmoji, | ||||||
|   elasticSearchMoviesAndShows |   elasticSearchMoviesAndShows | ||||||
| } | } | ||||||
|   | |||||||
| @@ -18,7 +18,7 @@ | |||||||
| </template> | </template> | ||||||
|  |  | ||||||
| <script> | <script> | ||||||
| import axios from 'axios' | import { register } from '@/api' | ||||||
| import SeasonedButton from '@/components/ui/SeasonedButton' | import SeasonedButton from '@/components/ui/SeasonedButton' | ||||||
| import SeasonedInput from '@/components/ui/SeasonedInput' | import SeasonedInput from '@/components/ui/SeasonedInput' | ||||||
| import SeasonedMessages from '@/components/ui/SeasonedMessages' | import SeasonedMessages from '@/components/ui/SeasonedMessages' | ||||||
| @@ -40,12 +40,9 @@ export default { | |||||||
|       let verifyCredentials = this.checkCredentials(username, password, passwordRepeat); |       let verifyCredentials = this.checkCredentials(username, password, passwordRepeat); | ||||||
|  |  | ||||||
|       if (verifyCredentials.verified) { |       if (verifyCredentials.verified) { | ||||||
|         axios.post(`https://api.kevinmidboe.com/api/v1/user`, { |  | ||||||
|           username: username, |         register(username, password) | ||||||
|           password: password |           .then(data => { | ||||||
|         }) |  | ||||||
|         .then(resp => { |  | ||||||
|           let data = resp.data; |  | ||||||
|             if (data.success){ |             if (data.success){ | ||||||
|               localStorage.setItem('token', data.token); |               localStorage.setItem('token', data.token); | ||||||
|               localStorage.setItem('username', username); |               localStorage.setItem('username', username); | ||||||
| @@ -56,7 +53,7 @@ export default { | |||||||
|             } |             } | ||||||
|         }) |         }) | ||||||
|         .catch(error => { |         .catch(error => { | ||||||
|           this.messages.push({ type: 'error', title: 'Unexpected error', message: error.response.data.error }) |           this.messages.push({ type: 'error', title: 'Unexpected error', message: error.message }) | ||||||
|         }); |         }); | ||||||
|       }  |       }  | ||||||
|       else { |       else { | ||||||
|   | |||||||
| @@ -16,7 +16,7 @@ | |||||||
|  |  | ||||||
|  |  | ||||||
| <script> | <script> | ||||||
| import axios from 'axios' | import { login } from '@/api' | ||||||
| import storage from '../storage' | import storage from '../storage' | ||||||
| import SeasonedInput from '@/components/ui/SeasonedInput' | import SeasonedInput from '@/components/ui/SeasonedInput' | ||||||
| import SeasonedButton from '@/components/ui/SeasonedButton' | import SeasonedButton from '@/components/ui/SeasonedButton' | ||||||
| @@ -39,23 +39,19 @@ export default { | |||||||
|       let username = this.username; |       let username = this.username; | ||||||
|       let password = this.password; |       let password = this.password; | ||||||
|  |  | ||||||
|       axios.post(`https://api.kevinmidboe.com/api/v1/user/login`, { |       login(username, password) | ||||||
|         username: username, |         .then(data => { | ||||||
|         password: password |  | ||||||
|       }) |  | ||||||
|       .then(resp => { |  | ||||||
|         let data = resp.data; |  | ||||||
|           if (data.success){ |           if (data.success){ | ||||||
|             localStorage.setItem('token', data.token); |             localStorage.setItem('token', data.token); | ||||||
|             localStorage.setItem('username', username); |             localStorage.setItem('username', username); | ||||||
|           localStorage.setItem('admin', data.admin); |             localStorage.setItem('admin', data.admin || false); | ||||||
|  |  | ||||||
|             eventHub.$emit('setUserStatus'); |             eventHub.$emit('setUserStatus'); | ||||||
|             this.$router.push({ name: 'profile' }) |             this.$router.push({ name: 'profile' }) | ||||||
|           } |           } | ||||||
|         }) |         }) | ||||||
|         .catch(error => { |         .catch(error => { | ||||||
|         if (error.message.endsWith('401')) { |           if (error.status === 401) { | ||||||
|             this.messages.push({ type: 'warning', title: 'Access denied', message: 'Incorrect username or password' }) |             this.messages.push({ type: 'warning', title: 'Access denied', message: 'Incorrect username or password' }) | ||||||
|           } |           } | ||||||
|           else { |           else { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user