se utiliza backend para login y logout
parent
60bc4738cb
commit
130fadb106
|
@ -0,0 +1 @@
|
|||
VITE_BACKEND="http://localhost:4000/api"
|
|
@ -11,6 +11,7 @@ node_modules
|
|||
dist
|
||||
dist-ssr
|
||||
*.local
|
||||
.env
|
||||
|
||||
# Editor directories and files
|
||||
.vscode/*
|
||||
|
|
|
@ -12,5 +12,6 @@ services:
|
|||
command: >
|
||||
sh -c "
|
||||
[ -d node_modules ] || npm i ;
|
||||
[ -f .env ] || cp .env.develop .env ;
|
||||
npm run dev
|
||||
"
|
|
@ -5,7 +5,7 @@
|
|||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite --host --port=3000",
|
||||
"build": "vite build",
|
||||
"build": "VITE_BACKEND= vite build",
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
|
|
@ -2,10 +2,15 @@
|
|||
import '../assets/navbar.css'
|
||||
import { Link } from 'svelte-navigator'
|
||||
import { storeCanasta } from '../stores/canasta';
|
||||
import { runLogout } from '../services/auth';
|
||||
|
||||
function onSalir() {
|
||||
sessionStorage.removeItem('usuario');
|
||||
document.location.href = '/'
|
||||
async function onSalir() {
|
||||
try {
|
||||
await runLogout()
|
||||
document.location.href = '/'
|
||||
} catch (error) {
|
||||
alert(error.detail || error)
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
|
|
18
src/main.js
18
src/main.js
|
@ -1,13 +1,21 @@
|
|||
import App from './layouts/Base.svelte'
|
||||
import AppLogin from './pages/site/Login.svelte'
|
||||
import { storeUsuario } from './stores/usuario';
|
||||
import { getInfoToken } from './services/auth';
|
||||
|
||||
let app;
|
||||
let usuario = JSON.parse(sessionStorage.getItem('usuario'))
|
||||
|
||||
if (usuario) {
|
||||
app = new App({ target: document.getElementById('app') })
|
||||
} else {
|
||||
app = new AppLogin({ target: document.getElementById('app') })
|
||||
async function begin() {
|
||||
try {
|
||||
const data = await getInfoToken()
|
||||
storeUsuario.set(data)
|
||||
app = new App({ target: document.getElementById('app') })
|
||||
} catch (error) {
|
||||
console.log(error.detail || error)
|
||||
app = new AppLogin({ target: document.getElementById('app') })
|
||||
}
|
||||
}
|
||||
|
||||
begin();
|
||||
|
||||
export default app
|
||||
|
|
|
@ -1,2 +1,8 @@
|
|||
<script>
|
||||
import { storeUsuario } from "../../stores/usuario";
|
||||
let nombre_usuario = '';
|
||||
$: nombre_usuario = `${$storeUsuario.first_name} ${$storeUsuario.last_name}`.trim() || $storeUsuario.username
|
||||
</script>
|
||||
|
||||
<h1>Pagina de inicio</h1>
|
||||
<p>Hola</p>
|
||||
<p>Hola: {nombre_usuario}</p>
|
|
@ -1,17 +1,18 @@
|
|||
<script>
|
||||
import { getToken } from "../../services/auth";
|
||||
import { runLogin } from "../../services/auth";
|
||||
|
||||
let form = {}
|
||||
let form = { username:null, password: null }
|
||||
let loading = false;
|
||||
let message = '';
|
||||
|
||||
async function onSubmit() {
|
||||
try {
|
||||
loading = true;
|
||||
const data = await getToken(form)
|
||||
sessionStorage.setItem('usuario', JSON.stringify(data))
|
||||
await runLogin(form)
|
||||
document.location.reload()
|
||||
} catch (error) {
|
||||
alert(error.message || error)
|
||||
message = error.detail || error
|
||||
setTimeout(() => message = '', 5000)
|
||||
} finally {
|
||||
loading = false;
|
||||
}
|
||||
|
@ -39,4 +40,8 @@
|
|||
</div>
|
||||
<button>Acceder</button>
|
||||
</form>
|
||||
|
||||
{#if message}
|
||||
<p>{message}</p>
|
||||
{/if}
|
||||
</div>
|
|
@ -0,0 +1 @@
|
|||
export const base = import.meta.env.VITE_BACKEND || 'api';
|
|
@ -1,18 +1,32 @@
|
|||
import { base } from './_config'
|
||||
|
||||
export function getToken({ username, password }) {
|
||||
return new Promise((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
if (username !== 'super') return reject("Usuario incorrecto");
|
||||
if (password !== 'super') return reject("Contaseña incorrecta");
|
||||
|
||||
const data = {
|
||||
username: 'super',
|
||||
first_name: 'Ronald',
|
||||
last_name: 'Morales',
|
||||
email: 'ronald@correo.com',
|
||||
token: '123445555'
|
||||
}
|
||||
resolve(data)
|
||||
}, 2000)
|
||||
export async function runLogin({ username, password }) {
|
||||
const res = await fetch(`${base}/auth/`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ username, password }),
|
||||
headers: { "Content-Type": "application/json" }
|
||||
})
|
||||
if (!res.ok) throw await res.json()
|
||||
const data = await res.json()
|
||||
document.cookie = `token=${data.token}`
|
||||
return data
|
||||
}
|
||||
|
||||
export async function runLogout() {
|
||||
const res = await fetch(`${base}/auth/logout/`, {
|
||||
method: 'POST'
|
||||
})
|
||||
if (!res.ok) throw await res.json()
|
||||
const data = await res.text()
|
||||
document.cookie = 'token=';
|
||||
return data;
|
||||
}
|
||||
|
||||
export async function getInfoToken() {
|
||||
const res = await fetch(`${base}/auth/info/`, {
|
||||
credentials: 'include'
|
||||
})
|
||||
if (!res.ok) throw await res.json()
|
||||
return await res.json()
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
import { writable } from 'svelte/store';
|
||||
|
||||
export const storeUsuario = writable(null)
|
Loading…
Reference in New Issue