Skip to content

Commit

Permalink
Tweak authentication logic
Browse files Browse the repository at this point in the history
  • Loading branch information
nzws committed May 25, 2024
1 parent 2543523 commit 7411bd6
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
4 changes: 2 additions & 2 deletions apps/server/src/middlewares/user-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import type { Middleware } from 'koa';
import { users } from '../models';

export const middlewareAuthorizeUser: Middleware = async (ctx, next) => {
const userId = ctx.state.userId as number;
const userId = ctx.state.userId as number | undefined;

const user = await users.get(userId);
if (!user) {
ctx.status = 401;
ctx.body = {
errorCode: 'user_not_found'
errorCode: 'unauthorized'
};
return;
}
Expand Down
3 changes: 3 additions & 0 deletions apps/server/src/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ export const Users = (prismaUser: PrismaClient['user']) =>
if (account) {
account = account.toLowerCase();
}
if (!id && !account) {
return undefined;
}

const user = await prismaUser.findUnique({
where: {
Expand Down
1 change: 1 addition & 0 deletions apps/web/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"navbar.logout": "Logout",
"auth.toast.login": "Successfully logged in",
"auth.toast.logout": "Successfully logged out",
"auth.toast.invalid-domain": "Invalid domain. The domain must be specified as \"example.com\" without any slashes.",
"toast.api-error.title": "An error has occurred",
"api-error.unauthorized": "Authentication failed: trying to re-login...",
"api-error.invalid_request": "Internal error: Invalid request. Please contact the administrator for more information.",
Expand Down
1 change: 1 addition & 0 deletions apps/web/locales/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"navbar.logout": "ログアウト",
"auth.toast.login": "ログインしました",
"auth.toast.logout": "ログアウトしました",
"auth.toast.invalid-domain": "無効なドメインです。ドメインは「example.com」のように、スラッシュなどは含めずに指定してください。",
"toast.api-error.title": "エラーが発生しました",
"api-error.unauthorized": "認証に失敗しました: 再ログインしています...",
"api-error.invalid_request": "内部エラー: 不正なリクエストです。詳しくは管理者にお問い合わせください",
Expand Down
21 changes: 19 additions & 2 deletions apps/web/organisms/navbar/login-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import {
Link,
useDisclosure,
Spinner,
Collapse
Collapse,
useToast
} from '@chakra-ui/react';
import isValidDomain from 'is-valid-domain';
import { ExternalLinkIcon } from '@chakra-ui/icons';
import { FormattedMessage, useIntl } from 'react-intl';
import { getDocsUrl } from '~/utils/constants';
Expand Down Expand Up @@ -48,6 +50,7 @@ const placeholderDomain: Record<ServerType, string> = {
export const LoginModal: FC<Props> = ({ isOpen, onClose }) => {
const { signIn } = useAuth();
const intl = useIntl();
const toast = useToast();
const {
isOpen: isOpeningSignIn,
onOpen: onOpenSignIn,
Expand All @@ -66,6 +69,14 @@ export const LoginModal: FC<Props> = ({ isOpen, onClose }) => {
setHasNotDomain(true);
return;
}
if (!isValidDomain(domain)) {
toast({
title: intl.formatMessage({ id: 'auth.toast.invalid-domain' }),
status: 'error',
isClosable: true
});
return;
}

onOpenSignIn();
if (serverType === ServerType.Mastodon) {
Expand All @@ -75,7 +86,7 @@ export const LoginModal: FC<Props> = ({ isOpen, onClose }) => {
}
onClose();
})();
}, [onOpenSignIn, onClose, signIn, domain, serverType]);
}, [signIn, domain, onOpenSignIn, serverType, onClose, toast, intl]);

useEffect(() => {
if (!isOpen) {
Expand Down Expand Up @@ -103,6 +114,12 @@ export const LoginModal: FC<Props> = ({ isOpen, onClose }) => {
}
}, [domain]);

useEffect(() => {
setServerType(undefined);
setDomain('');
setHasNotDomain(false);
}, [isOpen]);

return (
<Modal isOpen={isOpen} onClose={onClose} isCentered>
<ModalOverlay />
Expand Down

0 comments on commit 7411bd6

Please sign in to comment.