Added fetch api for login
This commit is contained in:
		
							parent
							
								
									509f940afd
								
							
						
					
					
						commit
						632ca94975
					
				
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
							
								
								
									
										53
									
								
								src/app.js
									
									
									
									
									
								
							
							
						
						
									
										53
									
								
								src/app.js
									
									
									
									
									
								
							@ -19,6 +19,7 @@ import router from '@/router'
 | 
				
			|||||||
import store from '@/store'
 | 
					import store from '@/store'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import { socket } from '@/utils/socket'
 | 
					import { socket } from '@/utils/socket'
 | 
				
			||||||
 | 
					import { get } from '@/utils/api'
 | 
				
			||||||
import { toast } from '@/utils/toasts'
 | 
					import { toast } from '@/utils/toasts'
 | 
				
			||||||
import { isValidURL } from '@/utils/utils'
 | 
					import { isValidURL } from '@/utils/utils'
 | 
				
			||||||
import { sendAddToQueue } from '@/utils/downloads'
 | 
					import { sendAddToQueue } from '@/utils/downloads'
 | 
				
			||||||
@ -86,26 +87,7 @@ socket.on('message', function(msg) {
 | 
				
			|||||||
	console.log(msg)
 | 
						console.log(msg)
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
socket.on('logging_in', function() {
 | 
					function loggedIn(data){
 | 
				
			||||||
	toast(i18n.t('toasts.loggingIn'), 'loading', false, 'login-toast')
 | 
					 | 
				
			||||||
})
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
socket.on('init_autologin', function() {
 | 
					 | 
				
			||||||
	let arl = localStorage.getItem('arl')
 | 
					 | 
				
			||||||
	let accountNum = localStorage.getItem('accountNum')
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if (arl) {
 | 
					 | 
				
			||||||
		arl = arl.trim()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
		if (accountNum != 0) {
 | 
					 | 
				
			||||||
			socket.emit('login', arl, true, accountNum)
 | 
					 | 
				
			||||||
		} else {
 | 
					 | 
				
			||||||
			socket.emit('login', arl)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
})
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
socket.on('logged_in', function(data) {
 | 
					 | 
				
			||||||
	const { status, user } = data
 | 
						const { status, user } = data
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	switch (status) {
 | 
						switch (status) {
 | 
				
			||||||
@ -138,6 +120,36 @@ socket.on('logged_in', function(data) {
 | 
				
			|||||||
		// $('#settings_picture').attr('src', `https://e-cdns-images.dzcdn.net/images/user/125x125-000000-80-0-0.jpg`)
 | 
							// $('#settings_picture').attr('src', `https://e-cdns-images.dzcdn.net/images/user/125x125-000000-80-0-0.jpg`)
 | 
				
			||||||
		// document.getElementById('home_not_logged_in').classList.remove('hide')
 | 
							// document.getElementById('home_not_logged_in').classList.remove('hide')
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fetch('connect').then(response => response.json()).then(data => {
 | 
				
			||||||
 | 
						store.dispatch('setAppInfo', data.update )
 | 
				
			||||||
 | 
						if (data.autologin) {
 | 
				
			||||||
 | 
							console.log("Autologin")
 | 
				
			||||||
 | 
							let arl = localStorage.getItem('arl')
 | 
				
			||||||
 | 
							let accountNum = localStorage.getItem('accountNum')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (arl) {
 | 
				
			||||||
 | 
								arl = arl.trim()
 | 
				
			||||||
 | 
								let result
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if (accountNum != 0) {
 | 
				
			||||||
 | 
									result = get('login', {arl: arl, force:true, child:accountNum || 0})
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									result = get('login', {arl})
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								result.then(loggedIn)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/*
 | 
				
			||||||
 | 
					socket.on('logging_in', function() {
 | 
				
			||||||
 | 
						toast(i18n.t('toasts.loggingIn'), 'loading', false, 'login-toast')
 | 
				
			||||||
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					socket.on('logged_in', function(data) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
socket.on('logged_out', function() {
 | 
					socket.on('logged_out', function() {
 | 
				
			||||||
@ -145,6 +157,7 @@ socket.on('logged_out', function() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	store.dispatch('logout')
 | 
						store.dispatch('logout')
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
					*/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
socket.on('restoringQueue', function() {
 | 
					socket.on('restoringQueue', function() {
 | 
				
			||||||
	toast(i18n.t('toasts.restoringQueue'), 'loading', false, 'restoring_queue')
 | 
						toast(i18n.t('toasts.restoringQueue'), 'loading', false, 'restoring_queue')
 | 
				
			||||||
 | 
				
			|||||||
@ -92,7 +92,6 @@ import { orderBy } from 'lodash-es'
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import { BaseTabs, BaseTab } from '@components/globals/BaseTabs'
 | 
					import { BaseTabs, BaseTab } from '@components/globals/BaseTabs'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import { socket } from '@/utils/socket'
 | 
					 | 
				
			||||||
import { sendAddToQueue } from '@/utils/downloads'
 | 
					import { sendAddToQueue } from '@/utils/downloads'
 | 
				
			||||||
import { checkNewRelease } from '@/utils/dates'
 | 
					import { checkNewRelease } from '@/utils/dates'
 | 
				
			||||||
import { formatArtistData, getArtistData } from '@/data/artist'
 | 
					import { formatArtistData, getArtistData } from '@/data/artist'
 | 
				
			||||||
 | 
				
			|||||||
@ -3,7 +3,7 @@ import VueRouter from 'vue-router'
 | 
				
			|||||||
import { socket } from '@/utils/socket'
 | 
					import { socket } from '@/utils/socket'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Pages
 | 
					// Pages
 | 
				
			||||||
import About from '@components/pages/About.vue'
 | 
					//import About from '@components/pages/About.vue'
 | 
				
			||||||
import InfoArl from '@components/pages/InfoArl.vue'
 | 
					import InfoArl from '@components/pages/InfoArl.vue'
 | 
				
			||||||
import InfoSpotifyFeatures from '@components/pages/InfoSpotifyFeatures.vue'
 | 
					import InfoSpotifyFeatures from '@components/pages/InfoSpotifyFeatures.vue'
 | 
				
			||||||
import Artist from '@components/pages/Artist.vue'
 | 
					import Artist from '@components/pages/Artist.vue'
 | 
				
			||||||
@ -81,11 +81,11 @@ const routes = [
 | 
				
			|||||||
		name: 'Link Analyzer',
 | 
							name: 'Link Analyzer',
 | 
				
			||||||
		component: LinkAnalyzer
 | 
							component: LinkAnalyzer
 | 
				
			||||||
	},
 | 
						},
 | 
				
			||||||
	{
 | 
						/*{
 | 
				
			||||||
		path: '/about',
 | 
							path: '/about',
 | 
				
			||||||
		name: 'About',
 | 
							name: 'About',
 | 
				
			||||||
		component: About
 | 
							component: About
 | 
				
			||||||
	},
 | 
						},*/
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		path: '/info-arl',
 | 
							path: '/info-arl',
 | 
				
			||||||
		name: 'ARL',
 | 
							name: 'ARL',
 | 
				
			||||||
@ -165,4 +165,3 @@ router.beforeEach((to, from, next) => {
 | 
				
			|||||||
})
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default router
 | 
					export default router
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										10
									
								
								src/utils/api.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								src/utils/api.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,10 @@
 | 
				
			|||||||
 | 
					export const get = function(key, data){
 | 
				
			||||||
 | 
					  let url = `/api/${key}`
 | 
				
			||||||
 | 
					  if (data){
 | 
				
			||||||
 | 
					    let query = Object.keys(data)
 | 
				
			||||||
 | 
					               .map(k => encodeURIComponent(k) + '=' + encodeURIComponent(data[k]))
 | 
				
			||||||
 | 
					               .join('&')
 | 
				
			||||||
 | 
					    url += '?'+query
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  return fetch(url).then(response => response.json())
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -3,19 +3,15 @@ import store from '@/store'
 | 
				
			|||||||
class CustomSocket extends WebSocket {
 | 
					class CustomSocket extends WebSocket {
 | 
				
			||||||
  constructor(args) {
 | 
					  constructor(args) {
 | 
				
			||||||
    super(args)
 | 
					    super(args)
 | 
				
			||||||
		console.log(args)
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	emit(key, data) {
 | 
						emit(key, data) {
 | 
				
			||||||
		console.log("emit:", key, data)
 | 
					 | 
				
			||||||
		console.log(this.readyState)
 | 
					 | 
				
			||||||
		if (this.readyState != WebSocket.OPEN) return false
 | 
							if (this.readyState != WebSocket.OPEN) return false
 | 
				
			||||||
		this.send(JSON.stringify({key:key, data:data}))
 | 
							this.send(JSON.stringify({key:key, data:data}))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	on(key, callback) {
 | 
						on(key, callback) {
 | 
				
			||||||
		this.addEventListener('message', function(event){
 | 
							this.addEventListener('message', function(event){
 | 
				
			||||||
			console.log(event.data)
 | 
					 | 
				
			||||||
			let data = JSON.parse(event.data)
 | 
								let data = JSON.parse(event.data)
 | 
				
			||||||
			if (data.key == key) callback(data.data)
 | 
								if (data.key == key) callback(data.data)
 | 
				
			||||||
		})
 | 
							})
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user