-
Notifications
You must be signed in to change notification settings - Fork 3
Add Pinia stores and dependencies. (Fixes #2) #15
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import { defineStore } from 'pinia' | ||
| import { ref, computed } from 'vue' | ||
| import { initOidc, getOidc } from '../services/auth.js' | ||
| import { JMAPClient } from '../services/jmap.js' | ||
| import { JMAP_SERVER_URL } from '../defines.js' | ||
|
|
||
| export const useAuthStore = defineStore('auth', () => { | ||
| const client = ref(null) | ||
| const connected = ref(false) | ||
| const status = ref('Not connected.') | ||
| const error = ref('') | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably be null. |
||
| const oidcReady = ref(false) | ||
| const oidcLoading = ref(true) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are these states independant of each other? You can merge it into an enum. (You're not using typescript but you can still create a object with the values.)
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ex/ const OIDC_LOADING_STATE = {
NOT_CONNECTED: 'not_connected',
FAILED: 'failed',
CONNECTING: 'connecting',
READY: 'ready',
};Set it in your code like so:
Then in your l10n you can simply have a computed that returns |
||
|
|
||
| const serverName = computed(() => { | ||
| try { | ||
| return new URL(JMAP_SERVER_URL).hostname | ||
| } catch { | ||
| return JMAP_SERVER_URL | ||
| } | ||
| }) | ||
|
|
||
| async function initializeOidc() { | ||
| try { | ||
| const oidc = await initOidc() | ||
| oidcReady.value = true | ||
| oidcLoading.value = false | ||
| return oidc | ||
| } catch (e) { | ||
| console.error('OIDC initialization failed:', e) | ||
| oidcLoading.value = false | ||
| return null | ||
| } | ||
| } | ||
|
|
||
| async function connect(credentials) { | ||
| if (!credentials?.username || !credentials?.password) { | ||
| error.value = 'Username and password required.' | ||
| return false | ||
| } | ||
| error.value = '' | ||
| status.value = 'Connecting…' | ||
| try { | ||
| client.value = new JMAPClient({ | ||
| baseUrl: JMAP_SERVER_URL, | ||
| username: credentials.username.trim(), | ||
| password: credentials.password, | ||
| }) | ||
| await client.value.fetchSession() | ||
| localStorage.setItem('jmap.username', credentials.username.trim()) | ||
| connected.value = true | ||
| document.body.classList.add('connected') | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be inferred based on the oidc status enum on whatever is consuming this store. No dom manip in stores! |
||
| return true | ||
| } catch (e) { | ||
| status.value = 'Failed.' | ||
| error.value = | ||
| e.message + | ||
| (e.message?.includes('Failed to fetch') | ||
| ? '\nLikely CORS/network issue.' | ||
| : '') | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| async function connectWithOAuth(oidc) { | ||
| error.value = '' | ||
| status.value = 'Connecting with OAuth…' | ||
| try { | ||
| client.value = new JMAPClient({ | ||
| baseUrl: JMAP_SERVER_URL, | ||
| getToken: async () => { | ||
| const { accessToken } = await oidc.getTokens() | ||
| return accessToken | ||
| }, | ||
| }) | ||
| await client.value.fetchSession() | ||
| connected.value = true | ||
| document.body.classList.add('connected') | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be inferred based on the oidc status enum on whatever is consuming this store. No dom manip in stores! |
||
| return true | ||
| } catch (e) { | ||
| status.value = 'Failed.' | ||
| error.value = | ||
| e.message + | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. es6 string literals pls. |
||
| (e.message?.includes('Failed to fetch') | ||
| ? '\nLikely CORS/network issue.' | ||
| : '') | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| async function handleOAuthLogin() { | ||
| const oidc = getOidc() | ||
| if (!oidc) return false | ||
| if (!oidc.isUserLoggedIn) { | ||
| await oidc.login() | ||
| return false | ||
| } | ||
| return await connectWithOAuth(oidc) | ||
| } | ||
|
|
||
| async function handleLogout() { | ||
| const oidc = getOidc() | ||
| if (oidc && oidc.isUserLoggedIn) { | ||
| await oidc.logout({ redirectTo: 'home' }) | ||
| } else { | ||
| window.location.reload() | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| client, | ||
| connected, | ||
| status, | ||
| error, | ||
| oidcReady, | ||
| oidcLoading, | ||
| serverName, | ||
| initializeOidc, | ||
| connect, | ||
| connectWithOAuth, | ||
| handleOAuthLogin, | ||
| handleLogout, | ||
| } | ||
| }) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These can be inferred based on an enum state I mentioned below.