Setup client authorization
This commit is contained in:
parent
0df0beea96
commit
0bf1be5c40
|
@ -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,
|
||||||
|
|
|
@ -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 }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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.')
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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 } : {}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue
Block a user