fix: initiate OAuth flow from callback route for correct redirect_uri

Changed files
+22 -4
src
components
services
+10
src/components/pages/grain-oauth-callback.js
··· 1 import { LitElement, html, css } from 'lit'; 2 import '../atoms/grain-spinner.js'; 3 4 export class GrainOAuthCallback extends LitElement { ··· 16 font-size: var(--font-size-sm); 17 } 18 `; 19 20 render() { 21 return html`
··· 1 import { LitElement, html, css } from 'lit'; 2 + import { auth } from '../../services/auth.js'; 3 import '../atoms/grain-spinner.js'; 4 5 export class GrainOAuthCallback extends LitElement { ··· 17 font-size: var(--font-size-sm); 18 } 19 `; 20 + 21 + async connectedCallback() { 22 + super.connectedCallback(); 23 + const params = new URLSearchParams(window.location.search); 24 + if (params.get('start') === '1') { 25 + window.history.replaceState({}, '', '/oauth/callback'); 26 + await auth.startOAuthFromCallback(); 27 + } 28 + } 29 30 render() { 31 return html`
+12 -4
src/services/auth.js
··· 60 61 async login(handle) { 62 sessionStorage.setItem('oauth_return_url', window.location.pathname); 63 - await this.#client.loginWithRedirect({ 64 - handle, 65 - redirectUri: `${window.location.origin}/oauth/callback` 66 - }); 67 } 68 69 logout() {
··· 60 61 async login(handle) { 62 sessionStorage.setItem('oauth_return_url', window.location.pathname); 63 + sessionStorage.setItem('oauth_handle', handle); 64 + window.location.href = '/oauth/callback?start=1'; 65 + } 66 + 67 + async startOAuthFromCallback() { 68 + const handle = sessionStorage.getItem('oauth_handle'); 69 + sessionStorage.removeItem('oauth_handle'); 70 + if (!handle) { 71 + router.replace('/'); 72 + return; 73 + } 74 + await this.#client.loginWithRedirect({ handle }); 75 } 76 77 logout() {