thombruce / credible-ruby-archived

JWT and API Token Authentication for Rails apps
MIT License
0 stars 0 forks source link

Credible.js #31

Open thombruce opened 4 years ago

thombruce commented 4 years ago

This is just a thought for now. There's more to do before Credible itself is ready, but I wouldn't mind thinking about it... A "Credible.js" package that drops into a frontend project and...

...and what?

Present approach (using Axios, Vue.js, Vue-Router and Vuex) is to... create store objects for tokens in Vuex, that are used by an interceptor in Axios, and the existence of which is checked by Vue-Router before rendering content.

Ideally, a Credible.js plugin would have no dependency on Vue, Vue-Router and Vuex... Axios is more debatable, but also... no.

And then the issue is... it isn't easily integrated into those.

It should:

  1. If an access-token exists in memory, attach this to all appropriate API calls

Actually... no... because the user might have a very different API they use the access token on, this is actually not appropriate. Scratch that.

It should:

  1. Receive login/signup/confirmation/reset_password credentials...
  2. Call a Credible API...
  3. Store a resultant refresh-token in the browser... (if remember me)
  4. Return an access token
  5. Refresh from refresh-token by calling the API

It should also...

  1. Expose information about refresh-token: expiry

User should be able to import Credible from 'credible' and utilise these features. E.g.

import Credible from 'credible'

session = new Credible({ config })

session.login(login: 'some_user', password: 's0me_pa$$word')
// should return access-token (will also set refresh-token)

session.refresh
// should return new access-token (will also set new refresh-token)
// fails if no refresh-token

session.accessToken
session.refreshToken

session.info
// should return decoded session info from access-token

config might accept settings like refreshable: true (possibly overridable if 'remember me: false', right?) Oh, and uhmm... I guess baseUrl: 'credible.someurl.com/auth' to allow user to set custom provider.

Reintegration with current setup

Given the above... which would store a refresh-token in browser and an access-token in memory...

...we wouldn't need to add authentication logic to the Vuex store anymore (this all sounds like it would be handled). Credible would just need to be imported by: Axios, Vue.js, Vue-Router so that:

  1. Axios could base an interceptor on the Credible session object's state.
  2. Vue.js could access session info.
  3. Vue-Router could use that session info to redirect where appropriate.

We'd be right back where we are... just with session management extracted.

And... is this pretty-much react-compatible too? Well... it's decoupled from Vue. It's state management would be separate... Yeah, I see no reason why this wouldn't just as easily drop into a React project, or other. I mean this should also work with server-side JS too... no, actually: one minor tweak though - storage of refresh-token.. would need adapted.

But yeah, I think a solid idea.