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

Added Button to Logout in Settings #68

Merged
merged 8 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
3 changes: 2 additions & 1 deletion src/application/i18n/messages/en.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"auth": {
"login": "Login"
"login": "Login",
"logout": "Logout"
},
"settings": {
"title": "Settings",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ interface UseOAuthComposableState {
* Shows a popup window with google authorization
*/
showGoogleAuthPopup: () => void;
/**
* Logs out the user
*/
logout: () => Promise<void>;
}

/**
* Methods for working with OAuth (Google)
* Methods for working with Auth
*/
export default function useOAuth(): UseOAuthComposableState {
export default function useAuth(): UseOAuthComposableState {
/**
* Google OAuth URL
*/
Expand Down Expand Up @@ -54,8 +58,16 @@ export default function useOAuth(): UseOAuthComposableState {
}
});
}
/**
*Logs out the user by deleting the refresh token in local strorage
*/
async function logout(): Promise<void> {
await authService.logout();
}

return {
showGoogleAuthPopup,
logout,
};
}

2 changes: 1 addition & 1 deletion src/application/services/useLayout.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { shallowRef, type ShallowRef, type Component, onBeforeMount, watch } from 'vue';
import { shallowRef, type ShallowRef, type Component, watch } from 'vue';
import { useRoute } from 'vue-router';
import Default from '@/presentation/layouts/Default.vue';
import Fullpage from '@/presentation/layouts/Fullpage.vue';
Expand Down
4 changes: 3 additions & 1 deletion src/domain/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type AuthRepository from '@/domain/auth.repository.interface';
import type EventBus from '@/domain/event-bus';
import { AuthCompletedEvent } from './event-bus/events/AuthCompleted';
import UnauthorizedError from './entities/errors/Unauthorized';
import { AuthLogoutEvent } from './event-bus/events/AuthLogoutEvent';

/**
* Business logic for Authentication
Expand Down Expand Up @@ -62,8 +63,9 @@ export default class AuthService {
await this.repository.removeSession();

/**
* @todo dispatch AuthLogoutEvent to notify all listeners about logout
* Event to notify all listeners about logout
*/
this.eventBus.dispatchEvent(new AuthLogoutEvent());
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/domain/event-bus/events/AuthLogoutEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* The name of event
*/
export const AUTH_LOGOUT_EVENT_NAME = 'auth-logout';

/**
* Use cross domain events to explicitly implement side effects of changes within of a domain
*/
export class AuthLogoutEvent extends CustomEvent<null> {
/**
* Constructor options
*/
constructor() {
super(AUTH_LOGOUT_EVENT_NAME, {
detail: null,
});
}
}
2 changes: 2 additions & 0 deletions src/domain/event-bus/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { AUTH_COMPLETED_EVENT_NAME, AuthCompletedEvent } from './events/AuthCompleted';
import type { AUTH_LOGOUT_EVENT_NAME, AuthLogoutEvent } from './events/AuthLogoutEvent';

/**
* Event Bus provides a loosely coupled communication way between Domain and some other layers
Expand All @@ -12,6 +13,7 @@ export default class EventBus extends EventTarget {};
*/
export type CrossDomainEventMap = {
[AUTH_COMPLETED_EVENT_NAME]: AuthCompletedEvent;
[AUTH_LOGOUT_EVENT_NAME]: AuthLogoutEvent;
};

/**
Expand Down
5 changes: 5 additions & 0 deletions src/domain/user.repository.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ export default interface UserRepositoryInterface {
* Return stored user data
*/
getUser: () => User | null;

/**
* Removes user data from the storage
*/
removeUser: () => Promise<void>;
}
7 changes: 7 additions & 0 deletions src/domain/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type UserRepository from '@/domain/user.repository.interface';
import type EventBus from '@/domain/event-bus';
import { AUTH_COMPLETED_EVENT_NAME } from './event-bus/events/AuthCompleted';
import type { User } from './entities/User';
import { AUTH_LOGOUT_EVENT_NAME } from './event-bus/events/AuthLogoutEvent';

/**
* Business logic for User
Expand All @@ -27,6 +28,12 @@ export default class UserService {
eventBus.addEventListener(AUTH_COMPLETED_EVENT_NAME, () => {
void this.repository.loadUser();
});
/**
* When we got unauthorized
*/
eventBus.addEventListener(AUTH_LOGOUT_EVENT_NAME, () => {
void this.repository.removeUser();
});
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/infrastructure/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import UserRepository from '@/infrastructure/user.repository';
import { UserStore } from '@/infrastructure/storage/user';
import type EventBus from '@/domain/event-bus';
import { AUTH_COMPLETED_EVENT_NAME, type AuthCompletedEvent } from '@/domain/event-bus/events/AuthCompleted';
import { AUTH_LOGOUT_EVENT_NAME } from '@/domain/event-bus/events/AuthLogoutEvent';

/**
* Repositories
Expand Down Expand Up @@ -74,6 +75,15 @@ export function init(noteApiUrl: string, eventBus: EventBus): Repositories {
notesApiTransport.continueAnonymous();
}
});
/**
* When we got unauthorized
*/
eventBus.addEventListener(AUTH_LOGOUT_EVENT_NAME, () => {
/**
* Tell API transport to continue working in anonymous mode (send waiting requests without auth)
*/
notesApiTransport.continueAnonymous();
});
neSpecc marked this conversation as resolved.
Show resolved Hide resolved

/**
* Init repositories
Expand Down
7 changes: 7 additions & 0 deletions src/infrastructure/storage/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@ export class UserStore extends SubscribableStore<UserStoreData> {
public setUser(user: User): void {
this.data.user = user;
}

/**
* Removes user data
*/
public removeUser(): void {
this.data.user = null;
}
}
8 changes: 7 additions & 1 deletion src/infrastructure/user.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,10 @@ export default class UserRepository extends Repository<UserStore, UserStoreData>
public getUser(): User | null {
return this.store.getUser();
}
}
/**
* Removes user data from the storage
*/
public async removeUser(): Promise<void> {
this.store.removeUser();
}
}
4 changes: 2 additions & 2 deletions src/presentation/components/header/HeaderLoginButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
import { IconUser } from '@codexteam/icons';
import { useI18n } from 'vue-i18n';
import Button from '@/presentation/components/button/Button.vue';
import useOAuth from '@/application/services/useOAuth';
import useAuth from '@/application/services/useAuth';

const { t } = useI18n();
const { showGoogleAuthPopup } = useOAuth();
const { showGoogleAuthPopup } = useAuth();

/**
* Shows Google Authentication in a popup
Expand Down
24 changes: 22 additions & 2 deletions src/presentation/pages/Settings.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
<template>
<h1>{{ t('settings.title') }}</h1>
<ThemeButton />
<Button
:text="t('auth.logout')"
type="primary"
:icon="IconUnlink"
@click="userLogout"
/>
</template>

<script lang="ts" setup>
import { useI18n } from 'vue-i18n';
import ThemeButton from '@/presentation/components/theme/ThemeButton.vue';
import ThemeButton from '../components/theme/ThemeButton.vue';
import Button from '../components/button/Button.vue';
import { IconUnlink } from '@codexteam/icons';
import { useRouter } from 'vue-router';
import useAuth from '@/application/services/useAuth';

const { t } = useI18n();
</script>
const router = useRouter();
const { logout } = useAuth();

/**
* Function to logout the user by deleting the refresh token from the local storage and redirecting to the main page
*/
async function userLogout() {
await logout().then(() => {
router.push({ path: '/' });
});
}
</script>
<style scoped>
</style>