Skip to content
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

Fixes some auth problems #126

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions layouts/default.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { useEvents } from "~/src/shared/lib/use-events";
import { useSettings } from "~/src/shared/lib/use-settings";
import SfdumpWrap from "~/src/shared/lib/vendor/dumper";
import { version } from "../package.json";
import {useProfileStore} from "~/stores/profile";
import { useSettingsStore } from "~/stores/settings";

export default defineComponent({
Expand All @@ -32,14 +33,14 @@ export default defineComponent({
SfdumpWrap(window.document);

const settingsStore = useSettingsStore();
const profileStore = useProfileStore();
const { themeType, isFixedHeader } = storeToRefs(settingsStore);

const {
api: { getVersion, getProfile },
api: { getVersion },
} = useSettings();

const apiVersion = await getVersion();
const profile = await getProfile();

const { events } = useEvents();

Expand All @@ -57,7 +58,7 @@ export default defineComponent({
? `v${apiVersion}`
: `@${apiVersion}`,
clientVersion,
profile,
profile: profileStore.profile,
};
},
});
Expand Down
28 changes: 18 additions & 10 deletions middleware/auth.global.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
import { useNuxtApp, navigateTo } from "#app"
import { useSettings } from "~/src/shared/lib/use-settings";
import { useProfileStore } from "~/stores/profile"

export default defineNuxtRouteMiddleware(async (to, from) => {
export default defineNuxtRouteMiddleware(async (to) => {
const app = useNuxtApp()
const {localStorage} = window;

if (!app.$appSettings.auth.enabled) {
return;
return
}

// todo: move token to a store
if (to.name !== 'login' && !app.$authToken.token) {
return navigateTo('/login');
const store = useProfileStore()
store.fetchToken()

if (store.isAuthenticated) {
const {api: {getProfile}} = useSettings();
const profile = await getProfile();
store.setProfile(profile)
return
}

if (to.name !== 'login' && !store.isAuthenticated) {
return navigateTo('/login')

Check failure on line 23 in middleware/auth.global.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Async arrow function expected no return value

Check failure on line 23 in middleware/auth.global.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Async arrow function expected no return value
}

if (to.name === 'login' && to?.query?.token) {
localStorage?.setItem('token', to.query.token);
// todo: use store
app.$authToken.token = to.query.token;
return navigateTo('/');
store.setToken(to.query.token)
return navigateTo('/')

Check failure on line 28 in middleware/auth.global.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Async arrow function expected no return value

Check failure on line 28 in middleware/auth.global.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Async arrow function expected no return value
}
})
9 changes: 8 additions & 1 deletion pages/login.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
<script setup lang="ts">
import { useNuxtApp, navigateTo } from "#app"

Check failure on line 2 in pages/login.vue

View workflow job for this annotation

GitHub Actions / build (18.x)

You cannot import layer "app" into "pages" (shared -> entities -> features -> widgets -> pages -> processes -> app)

Check failure on line 2 in pages/login.vue

View workflow job for this annotation

GitHub Actions / build (20.x)

You cannot import layer "app" into "pages" (shared -> entities -> features -> widgets -> pages -> processes -> app)
import { REST_API_URL } from "~/src/shared/lib/io";
import { IconSvg } from "~/src/shared/ui";
import {useProfileStore} from "~/stores/profile";

definePageMeta({

Check failure on line 7 in pages/login.vue

View workflow job for this annotation

GitHub Actions / build (18.x)

'definePageMeta' is not defined

Check failure on line 7 in pages/login.vue

View workflow job for this annotation

GitHub Actions / build (20.x)

'definePageMeta' is not defined
layout: 'blank'
})

const app = useNuxtApp()
const store = useProfileStore()

if (store.isAuthenticated) {
await navigateTo('/')
}

const redirect = async () => {
await navigateTo(`${REST_API_URL}/${app.$appSettings.auth.login_url}`, {
Expand All @@ -29,7 +35,8 @@
Continue to SSO
</button>
</div>
<div class="login-form-right-block"
<div
class="login-form-right-block"
style="background: url('/bg.jpg'); background-size: cover; background-position: center center;">
</div>
</div>
Expand Down
2 changes: 2 additions & 0 deletions pages/settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@
const { changeTheme, changeNavbar } = settingsStore;
const { themeType, isFixedHeader } = storeToRefs(settingsStore);

console.log(settingsStore)

Check failure on line 82 in pages/settings.vue

View workflow job for this annotation

GitHub Actions / build (18.x)

Unexpected console statement

Check failure on line 82 in pages/settings.vue

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected console statement

return {
themeType,
isFixedHeader,
Expand Down
4 changes: 2 additions & 2 deletions pages/smtp/[id].vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { useFetch, useNuxtApp, useRoute, useRouter } from "#app"; // eslint-disa
import { PageHeader } from "~/src/widgets/ui";
import { useSmtp } from "~/src/entities/smtp";
import type { SMTP } from "~/src/entities/smtp/types";
import { REST_API_URL } from "~/src/shared/lib/io";
import { htmlEncode } from "~/src/shared/lib/helpers";
import { useEvents } from "~/src/shared/lib/use-events";
import type { EventId, ServerEvent } from "~/src/shared/types";
import { SmtpPage } from "~/src/screens/smtp";
Expand Down Expand Up @@ -63,7 +63,7 @@ export default defineComponent({
serverEvent: event,
pending,
eventId,
html: `<iframe src="${REST_API_URL}/api/smtp/${eventId}/html"/>`,
html: `<iframe srcdoc="${htmlEncode(event.value.payload.html)}"/>`,
clearEvent: () => events.removeById(eventId),
};
},
Expand Down
12 changes: 4 additions & 8 deletions plugins/auth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { useSettings } from "~/src/shared/lib/use-settings";

const {localStorage} = window;

// todo: use store for token
export default defineNuxtPlugin(async () => {
const {
Expand All @@ -25,18 +23,16 @@ export default defineNuxtPlugin(async () => {
if (!settings.auth.enabled) {
return {
provide: {
authToken: {token: null},
appSettings: settings
appSettings: settings,
authToken: {token: null}
}
}
}

const token: string | null = localStorage?.getItem('token')

return {
provide: {
authToken: {token},
appSettings: settings
appSettings: settings,
authToken: {token: null}
}
}
})
4 changes: 2 additions & 2 deletions src/entities/ray/mocks/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import rayBooleanFalseMock from './ray-boolean-false.json';
import rayBooleanTrueMock from './ray-boolean-true.json';
import rayCallerMock from './ray-caller.json';
import rayCarbonMock from './ray-carbon.json';
import rayColorMock from './ray-color.json';
Expand All @@ -18,8 +20,6 @@ import raySizeMock from './ray-size.json';
import rayTableMock from './ray-table.json';
import rayTextMock from './ray-text.json';
import rayTraceMock from './ray-trace.json';
import rayBooleanTrueMock from './ray-boolean-true.json';
import rayBooleanFalseMock from './ray-boolean-false.json';

export {
rayCallerMock,
Expand Down
1 change: 1 addition & 0 deletions src/entities/smtp/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface SMTPUser {
name: string,
email: string,
}

export interface SMTP {
id: string,
from: SMTPUser[],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Meta, StoryObj } from "@storybook/vue3";
import type { ComponentProps } from "vue-component-type-helpers";
import {htmlEncode} from "~/src/shared/lib/helpers";
import { HTMLCode } from '~/src/shared/mocks'
import SmtpPagePreview from './smtp-page-preview.vue';

Expand All @@ -13,7 +14,11 @@ export default {
args,
};
},
template: `<SmtpPagePreview v-bind="args">${HTMLCode}</SmtpPagePreview>`,
template: `<div style="height: 100vh">
<SmtpPagePreview v-bind="args">
<iframe srcdoc="<html>${htmlEncode(HTMLCode)}</html>"></iframe>
</SmtpPagePreview>
</div>`,
})
} as Meta<typeof SmtpPagePreview>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const currentDevice = ref(props.device);
}

.smtp-page-preview__device {
@apply flex-1 flex flex-col items-center bg-gray-50 dark:bg-gray-900;
@apply flex flex-col items-center bg-gray-50 dark:bg-gray-900;

html.dark & {
@apply text-gray-800;
Expand Down
2 changes: 1 addition & 1 deletion src/screens/smtp/ui/smtp-page/smtp-page.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import moment from "moment";
import { computed, ref } from "vue";
import { Tab, Tabs } from "vue3-tabs-component";
import { SMTP } from "~/src/entities/smtp/types";
import type { SMTP } from "~/src/entities/smtp/types";
import type { NormalizedEvent, Attachment } from "~/src/shared/types";
import {
TableBase,
Expand Down
3 changes: 3 additions & 0 deletions src/shared/lib/helpers/htmlEncode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function htmlEncode(input: string): string {
return input.replace(/./gm, (s: string) => (s.match(/[a-z0-9\s]+/i)) ? s : `&#${ s.charCodeAt(0) };`);
}
1 change: 1 addition & 0 deletions src/shared/lib/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './htmlEncode';
4 changes: 2 additions & 2 deletions src/shared/lib/io/use-events-requests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useNuxtApp } from "#app"

Check failure on line 1 in src/shared/lib/io/use-events-requests.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

You cannot import layer "app" into "shared" (shared -> entities -> features -> widgets -> pages -> processes -> app)

Check failure on line 1 in src/shared/lib/io/use-events-requests.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

You cannot import layer "app" into "shared" (shared -> entities -> features -> widgets -> pages -> processes -> app)
import type { EventId, EventType, ServerEvent } from '../../types';
import { REST_API_URL } from "./constants";
import { useNuxtApp } from "#app"

type TUseEventsRequests = () => {
getAll: () => Promise<ServerEvent<unknown>[]>,
Expand All @@ -16,7 +16,7 @@

export const useEventsRequests: TUseEventsRequests = () => {
const app = useNuxtApp()
const token: string | null = app.$authToken.token
const {token} = app.$authToken
const headers = {"X-Auth-Token": token}
const getEventRestUrl = (param?: string): string => `${REST_API_URL}/api/event${param ? `/${param}` : 's'}`

Expand Down
5 changes: 2 additions & 3 deletions src/shared/lib/use-api-transport/use-api-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import { useCentrifuge, useEventsRequests } from "../io";
import { useConnectionStore } from "~/stores/connections";
import { useEventStore } from "~/stores/events";

const CHECK_CONNECTION_INTERVAL: number = 10000
let isEventsEmitted: boolean = false
let isEventsEmitted = false

export const useApiTransport = () => {
const nuxtApp = useNuxtApp()
const token = nuxtApp.$authToken.token
const {token} = nuxtApp.$authToken
const {centrifuge} = useCentrifuge()
const eventsStore = useEventStore()
const connectionStore = useConnectionStore()
Expand Down
1 change: 1 addition & 0 deletions src/shared/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./events";
export * from "./profile";
export * from "./generics";
export * from "./local-storage";
export * from "./partials";
5 changes: 5 additions & 0 deletions src/shared/types/profile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface Profile {
username: string,
email: string,
avatar: string,
}
4 changes: 1 addition & 3 deletions src/shared/ui/icon-svg/icon-svg-originals/logout.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading