Setup client authorization

This commit is contained in:
Douglas Barone 2023-10-26 10:31:51 -04:00
parent 0df0beea96
commit 0bf1be5c40
5 changed files with 19 additions and 7 deletions

View File

@ -26,7 +26,7 @@ export async function login(
await pa.login({ username, ip, domain: user.domain }) await pa.login({ username, ip, domain: user.domain })
const jwt = await jwtService.generateToken({ const jwt = jwtService.generateToken({
displayName: user.displayName, displayName: user.displayName,
username: user.username, username: user.username,
domain: user.domain, domain: user.domain,

View File

@ -16,6 +16,8 @@ function createContext({ req, res }: trpcExpress.CreateExpressContextOptions) {
const jwtPayload = token ? jwtService.verifyToken(token) : null const jwtPayload = token ? jwtService.verifyToken(token) : null
console.log(jwtPayload)
return { ip, user: jwtPayload } return { ip, user: jwtPayload }
} }

View File

@ -43,7 +43,7 @@ import { PropType } from 'vue'
import { trpc } from '../trpc' import { trpc } from '../trpc'
import { LoginResult } from '@/server/schemas/Auth' import { LoginResult } from '@/server/schemas/LoginResult'
const props = defineProps({ const props = defineProps({
loginResult: { loginResult: {
@ -58,10 +58,9 @@ async function onLogout() {
'Deseja realmente sair? Você será desconectado e não poderá navegar na Internet até fazer login novamente.' 'Deseja realmente sair? Você será desconectado e não poderá navegar na Internet até fazer login novamente.'
) )
) { ) {
const success = await trpc.logout.mutate({ const success = await trpc.logout.mutate()
username: props.loginResult.username,
domain: props.loginResult.domain localStorage.removeItem('token')
})
if (success) { if (success) {
alert('Você foi desconectado.') alert('Você foi desconectado.')

View File

@ -65,9 +65,12 @@ import { ref, computed } from 'vue'
import { trpc } from '../trpc' import { trpc } from '../trpc'
import { VForm } from 'vuetify/lib/components/index.mjs' import { VForm } from 'vuetify/lib/components/index.mjs'
import LoginErrorAlert from './LoginErrorAlert.vue' import LoginErrorAlert from './LoginErrorAlert.vue'
import { useToken } from '../composables/useToken'
const emit = defineEmits(['login']) const emit = defineEmits(['login'])
const { token } = useToken()
const username = ref('') const username = ref('')
const password = ref('') const password = ref('')
@ -97,6 +100,10 @@ async function doLogin() {
emit('login', loginResult) emit('login', loginResult)
console.log(loginResult) console.log(loginResult)
if (loginResult.token) {
token.value = loginResult.token
}
} catch (e: any) { } catch (e: any) {
console.log(e.message) console.log(e.message)
error.value = e.message error.value = e.message

View File

@ -8,7 +8,11 @@ const SERVER_URL = import.meta.env.VITE_SERVER_URL || ''
export const trpc = createTRPCProxyClient<AppRouter>({ export const trpc = createTRPCProxyClient<AppRouter>({
links: [ links: [
httpBatchLink({ httpBatchLink({
url: `${SERVER_URL}/trpc` url: `${SERVER_URL}/trpc`,
headers() {
const token = localStorage.getItem('token')
return token ? { Authorization: token } : {}
}
}) })
] ]
}) })