Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#25 Login state is persisted on refresh #24

Merged
merged 3 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions frontend/layouts/default.vue
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ export default {
}
},
mounted () {
this.$wikibase.$oauth.autoLoginByCookie()
window.addEventListener('keydown', this.keyDownHandler)
},
destroyed () {
Expand Down Expand Up @@ -193,6 +194,7 @@ export default {
logout () {
this.$store.commit('auth/logout')
this.$notification.success(this.$i18n.t('auth.logout.success'))
this.$cookies.remove('oauth')
}
}
}
Expand Down
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@nuxtjs/toast": "^3.3.1",
"cookie-universal-nuxt": "^2.2.2",
"core-js": "^3.26.1",
"jsonwebtoken": "8.5.1",
"nuxt": "^2.15.8",
"nuxt-material-design-icons": "^1.0.4",
"vuetify": "^2.6.12",
Expand Down
29 changes: 27 additions & 2 deletions frontend/service/oauth.service.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const jwt = require('jsonwebtoken')

export class OAuthService {
constructor (store, config) {
constructor (store, app) {
this.$store = store
this.$config = config
this.$config = app.$config
this.$cookies = app.$cookies
}

step1 () {
Expand Down Expand Up @@ -41,10 +44,32 @@ export class OAuthService {
const accessToken = response.accessToken
const username = await this.getUsername(accessToken)
this.$store.commit('auth/login', { username, accessToken })
const oauth = {
username,
accessToken
}
const signer = 'password'
const token = jwt.sign(oauth, signer)
this.$cookies.set('oauth', token, {
path: '/',
maxAge: 60 * 60
})
}
return response
}

autoLoginByCookie () {
const token = this.$cookies.get('oauth')
try {
const decoded = jwt.verify(token, 'password')
const username = decoded.username
const accessToken = decoded.accessToken
this.$store.commit('auth/login', { username, accessToken })
} catch (err) {
console.error(err)
}
}

getUsername (accessToken) {
return fetch(`${this.$config.apiBaseUrl}/api/oauth/username?oauth_token=${accessToken.token}&oauth_tokensecret=${accessToken.tokenSecret}`)
.then((response) => {
Expand Down
2 changes: 1 addition & 1 deletion frontend/service/wikibase.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class WikibaseService {
})
this.$store = store
this.$query = new QueryService(store, this.$config)
this.$oauth = new OAuthService(store, this.$config)
this.$oauth = new OAuthService(store, app)
}

getWbk () {
Expand Down