Skip to content

Commit

Permalink
Debugging auth (#241)
Browse files Browse the repository at this point in the history
## Изменения
Изменил формат authButtons с учетом разных форм записи authMethods на
фронте и апи.
  • Loading branch information
BatuevIO authored Sep 29, 2024
1 parent 5d9df64 commit 0dbd34f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 20 deletions.
7 changes: 4 additions & 3 deletions src/components/IrdomAuthButton.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { AuthMethodName } from '@/models';
import { AuthMethodLink, AuthMethodName } from '@/models';
import apiClient from '@/api/';
export interface AuthButton {
name: string;
link: AuthMethodLink;
method: AuthMethodName;
icon?: string;
color?: string;
Expand All @@ -20,15 +21,15 @@ const props = withDefaults(defineProps<Props>(), { unlink: false });
const authUrl = ref<string | null>(null);
onMounted(async () => {
const { data } = await apiClient.GET(`/auth/${props.button.method}/auth_url`);
const { data } = await apiClient.GET(`/auth/${props.button.link}/auth_url`);
if (data) {
authUrl.value = data.url;
}
});
async function clickHandler() {
if (props.unlink) {
await apiClient.DELETE(`/auth/${props.button.method}`);
await apiClient.DELETE(`/auth/${props.button.link}`);
location.reload(); // TODO: придумать нормальное решение
} else if (authUrl.value) {
window.open(authUrl.value, '_self');
Expand Down
21 changes: 14 additions & 7 deletions src/constants/authButtons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,50 @@ import { AuthButton } from '@/components/IrdomAuthButton.vue';
export const authButtons: AuthButton[] = [
{
name: 'ЛК МГУ',
method: 'lk-msu',
link: 'lk-msu',
method: 'lkmsu_auth',
icon: `${logos}#msu`,
color: '#58b4470d',
},
{
name: '@physics.msu.ru',
method: 'physics-msu',
link: 'physics-msu',
method: 'physics_auth',
icon: `${logos}#ff`,
color: '#00014c0d',
},
{
name: '@my.msu.ru',
method: 'my-msu',
link: 'my-msu',
method: 'mymsu_auth',
icon: `${logos}#msu`,
color: '#2f39500d',
},
{
name: 'Yandex',
method: 'yandex',
link: 'yandex',
method: 'yandex_auth',
icon: `${logos}#yandex`,
color: '#e94c000d',
},
{
name: 'ВК',
method: 'vk',
link: 'vk',
method: 'vk_auth',
icon: `${logos}#vk`,
color: '#0077ff0d',
},
{
name: 'Github',
method: 'github',
link: 'github',
method: 'github_auth',
icon: `${logos}#github`,
color: '#24292f0d',
},
{
name: 'Google',
method: 'google',
link: 'google',
method: 'google_auth',
icon: `${logos}#google`,
color: '#58b4470d',
},
Expand Down
14 changes: 13 additions & 1 deletion src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export enum AuthMethod {
VK = 'vk',
}

export const AuthMethodNameList = [
export const AuthMethodLinkList = [
'github',
'google',
'vk',
Expand All @@ -31,6 +31,18 @@ export const AuthMethodNameList = [
'my-msu',
'physics-msu',
] as const;
export type AuthMethodLink = (typeof AuthMethodLinkList)[number];

export const AuthMethodNameList = [
'email',
'github_auth',
'google_auth',
'vk_auth',
'lkmsu_auth',
'yandex_auth',
'mymsu_auth',
'physics_auth',
] as const;
export type AuthMethodName = (typeof AuthMethodNameList)[number];

export type MySessionInfo =
Expand Down
8 changes: 4 additions & 4 deletions src/router/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export const authHandler: NavigationGuard = async to => {
const toastStore = useToastStore();

if (to.path.startsWith('/auth/oauth-authorized')) {
const methodName = to.params.method;
if (!isAuthMethod(methodName)) {
const methodLink = to.params.link;
if (!isAuthMethod(methodLink)) {
return {
path: '/auth/error',
query: { text: 'Метод авторизации не существует' },
Expand All @@ -62,7 +62,7 @@ export const authHandler: NavigationGuard = async to => {
};
}

const { data, response } = await apiClient.POST(`/auth/${methodName}/registration`, {
const { data, response } = await apiClient.POST(`/auth/${methodLink}/registration`, {
body: {
...to.query,
session_name: navigator.userAgent ?? UNKNOWN_DEVICE,
Expand All @@ -89,7 +89,7 @@ export const authHandler: NavigationGuard = async to => {
}

sessionStorage.setItem('id-token', id_token);
sessionStorage.setItem('id-token-issuer', methodName);
sessionStorage.setItem('id-token-issuer', methodLink);
return { path: '/auth/register-oauth', replace: true };
}

Expand Down
6 changes: 3 additions & 3 deletions src/utils/authMethodName.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AuthMethodName, AuthMethodNameList } from '@/models';
import { AuthMethodLink, AuthMethodLinkList } from '@/models';

export function isAuthMethod(authMethod: string | string[]): authMethod is AuthMethodName {
return AuthMethodNameList.includes(authMethod as AuthMethodName);
export function isAuthMethod(authMethod: string | string[]): authMethod is AuthMethodLink {
return AuthMethodLinkList.includes(authMethod as AuthMethodLink);
}
4 changes: 2 additions & 2 deletions src/views/auth/OauthRegisterView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useRouter } from 'vue-router';
import IrdomLayout from '@/components/IrdomLayout.vue';
import { useToolbar } from '@/store/toolbar';
import { useProfileStore } from '@/store/profile';
import { AuthMethodName, UNKNOWN_DEVICE } from '@/models';
import { AuthMethodLink, UNKNOWN_DEVICE } from '@/models';
import apiClient from '@/api/';
const router = useRouter();
Expand All @@ -18,7 +18,7 @@ toolbar.setup({
async function handleAccept() {
try {
const idToken = sessionStorage.getItem('id-token');
const idTokenIssuer = sessionStorage.getItem('id-token-issuer') as AuthMethodName;
const idTokenIssuer = sessionStorage.getItem('id-token-issuer') as AuthMethodLink;
if (!idToken || !idTokenIssuer) {
return router.replace({ path: '/auth/error', query: { text: 'Непредвиденная ошибка' } });
Expand Down

0 comments on commit 0dbd34f

Please sign in to comment.