implement verifyLogin

This commit is contained in:
Ulf Gebhardt 2021-11-23 14:05:02 +01:00
parent e9ceb7e7a5
commit 636c4c8df3
Signed by: ulfgebhardt
GPG Key ID: DA6B843E748679C9
5 changed files with 64 additions and 5 deletions

View File

@ -25,7 +25,6 @@ import moment from 'vue-moment'
const httpLink = new HttpLink({ uri: CONFIG.GRAPHQL_URI })
const authLink = new ApolloLink((operation, forward) => {
const token = store.state.token
operation.setContext({

View File

@ -224,6 +224,39 @@ export class UserResolver {
}
*/
@Authorized()
@Query(() => User)
@UseMiddleware(klicktippNewsletterStateMiddleware)
async verifyLogin(@Ctx() context: any): Promise<User> {
// TODO refactor and do not have duplicate code with login(see below)
const userRepository = getCustomRepository(UserRepository)
const userEntity = await userRepository.findByPubkeyHex(context.pubKey)
const loginUserRepository = getCustomRepository(LoginUserRepository)
const loginUser = await loginUserRepository.findByEmail(userEntity.email)
const user = new User()
user.email = userEntity.email
user.firstName = userEntity.firstName
user.lastName = userEntity.lastName
user.username = userEntity.username
user.description = loginUser.description
user.pubkey = userEntity.pubkey.toString('hex')
user.language = loginUser.language
// Elopage Status & Stored PublisherId
user.hasElopage = await this.hasElopage(context)
// coinAnimation
const userSettingRepository = getCustomRepository(UserSettingRepository)
const coinanimation = await userSettingRepository
.readBoolean(userEntity.id, Setting.COIN_ANIMATION)
.catch((error) => {
throw new Error(error)
})
user.coinanimation = coinanimation
user.isAdmin = true // TODO implement
return user
}
@Query(() => User)
@UseMiddleware(klicktippNewsletterStateMiddleware)
async login(
@ -295,7 +328,7 @@ export class UserResolver {
throw new Error(error)
})
user.coinanimation = coinanimation
user.isAdmin = true // TODO implement
user.isAdmin = true // TODO implement
context.setHeaders.push({
key: 'token',

View File

@ -20,6 +20,26 @@ export const login = gql`
}
`
export const verifyLogin = gql`
query {
verifyLogin {
email
username
firstName
lastName
language
description
coinanimation
klickTipp {
newsletterState
}
hasElopage
publisherId
isAdmin
}
}
`
export const logout = gql`
query {
logout

View File

@ -51,7 +51,7 @@ Vue.config.productionTip = false
loadAllRules(i18n)
addNavigationGuards(router, store)
addNavigationGuards(router, store, apolloProvider.defaultClient)
/* eslint-disable no-new */
new Vue({

View File

@ -1,4 +1,6 @@
const addNavigationGuards = (router, store) => {
import { verifyLogin } from '../graphql/queries'
const addNavigationGuards = (router, store, apollo) => {
// handle publisherId
router.beforeEach((to, from, next) => {
const publisherId = to.query.pid
@ -10,10 +12,15 @@ const addNavigationGuards = (router, store) => {
})
// store token on authenticate
router.beforeEach((to, from, next) => {
router.beforeEach(async (to, from, next) => {
if (to.path === '/authenticate' && to.query.token) {
// TODO verify user in order to get user data
store.commit('token', to.query.token)
const result = await apollo.query({
query: verifyLogin,
fetchPolicy: 'network-only',
})
store.dispatch('login', result.data.verifyLogin)
next({ path: '/overview' })
} else {
next()