apollo icon indicating copy to clipboard operation
apollo copied to clipboard

Apollo onLogin token expiration in seconds

Open websitevirtuoso opened this issue 4 years ago • 4 comments

What problem does this feature solve?

By documentation we have this.$apolloHelpers.onLogin(token /* if not default you can pass in client as second argument, you can set custom cookies attributes object as the third argument, and you can skip reset store as the fourth argument */)

We can set up ttl in days. But my App using ttl in seconds (2-5 hours for example)

Right now I extend my middleware like this one

export default function ({ app, error }) {
  const tokenExpireDateTime = app.$cookies.get(AUTH_TOKEN_EXPIRE)
  const tokenTtl = app.$moment(tokenExpireDateTime).diff(app.$moment(), 'seconds')
  const hasToken = !!app.$apolloHelpers.getToken()
  if (!hasToken || tokenTtl < 0) {
    error({ statusCode: 403, message: 'Permission denied', description: 'Sorry, you are forbidden from accessing this page.' })
  }

It solves a problem but anyway the best way use ttl in seconds

This feature request is available on Nuxt community (#c290)

websitevirtuoso avatar Feb 02 '20 07:02 websitevirtuoso

Hello @websitevirtuoso,

I was looking for how to handle cookies expiration and found your issue. Are you making an additional cookie for the expiration date?

I wasted a bounty on SO for this, and I am still very confused about how it was meant to deal with. The auth example is incomplete as it does not cover this, nor the doc.

I would really appreciate advice on this.

Billybobbonnet avatar Feb 27 '20 10:02 Billybobbonnet

Yes right I have to make an additional cookie for the expiration date.

I will share my code to try help you https://gist.github.com/websitevirtuoso/f3681abd3f62520d32cce2b1ac1cfeed https://gist.github.com/websitevirtuoso/ff50344ca97f01bbcbaef6d3dbe48226

Hope it will help you. Let me know if you have any questions

websitevirtuoso avatar Feb 27 '20 23:02 websitevirtuoso

Sorry for the late answer! Thank you very much for your help. I'll start looking into it and let youknow how it goes.

Thank you again :smiley:

Billybobbonnet avatar Mar 12 '20 11:03 Billybobbonnet

Hey there, some notes from my side:

The expire time only gets interpreted as days, if you provide a number object. You can also set a Date object:

this.$apolloHelpers.onLogin(response.token, undefined, { expires: new Date(1612718343000), path: '/' }, false);     

philipptrenz avatar Feb 07 '21 17:02 philipptrenz