Optimize the auth flow (#68)
Co-authored-by: AceDataCloud <office@acedata.cloud>
This commit is contained in:
1 parent
033cbc082c
commit
85aef535f6
23 files changed
+215
-245
No files matched your search
@@ -1,46 +1,20 @@
|
||||
<script lang="ts">
|
||||
import { defineComponent } from 'vue';
|
||||
import { IUser, IToken } from '@/models';
|
||||
|
||||
interface IData {
|
||||
accessToken: string | undefined;
|
||||
refreshToken: string | undefined;
|
||||
redirect: string | undefined;
|
||||
code: string | undefined;
|
||||
user: IUser | undefined;
|
||||
}
|
||||
|
||||
export default defineComponent({
|
||||
name: 'AuthCallback',
|
||||
data(): IData {
|
||||
return {
|
||||
accessToken: undefined,
|
||||
refreshToken: undefined,
|
||||
redirect: this.$route.query.redirect?.toString(),
|
||||
code: this.$route.query.code?.toString(),
|
||||
user: undefined
|
||||
redirect: this.$route.query.redirect?.toString()
|
||||
};
|
||||
},
|
||||
async mounted() {
|
||||
// if auth code is provided, get token via oauth
|
||||
if (this.code) {
|
||||
const data: IToken = await this.$store.dispatch('getToken', this.code);
|
||||
this.accessToken = data.access;
|
||||
this.refreshToken = data.refresh;
|
||||
} else {
|
||||
this.$store.dispatch('resetToken');
|
||||
return;
|
||||
}
|
||||
// if token acquired, get user info
|
||||
if (this.accessToken && this.refreshToken) {
|
||||
const user = await this.$store.dispatch('getUser');
|
||||
this.user = user;
|
||||
if (this.redirect) {
|
||||
await this.$router.push(this.redirect);
|
||||
}
|
||||
} else {
|
||||
// reset token and trigger popup login window
|
||||
this.$store.dispatch('resetToken');
|
||||
if (this.redirect) {
|
||||
await this.$router.push(this.redirect);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<script lang="ts">
|
||||
import { getBaseUrlAuth, getBaseUrlHub } from '@/utils';
|
||||
import { login } from '@/utils';
|
||||
import { defineComponent } from 'vue';
|
||||
|
||||
interface IData {
|
||||
@@ -14,13 +14,7 @@ export default defineComponent({
|
||||
};
|
||||
},
|
||||
async mounted() {
|
||||
const hubBaseUrl = getBaseUrlHub();
|
||||
const authBaseUrl = getBaseUrlAuth();
|
||||
// callback url used to init access token and then redirect back of `redirect`
|
||||
const callbackUrl = `${hubBaseUrl}/auth/callback?redirect=${this.redirect}`;
|
||||
// redirect to auth service to get access token then redirect back
|
||||
const targetUrl = `${authBaseUrl}/auth/login?redirect=${callbackUrl}`;
|
||||
window.location.href = targetUrl;
|
||||
login(this.redirect);
|
||||
},
|
||||
methods: {}
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user