move login redirects to client app
This commit is contained in:
parent
29072fb801
commit
3542bfcf17
@ -26,38 +26,16 @@ export class AuthController {
|
||||
}
|
||||
}
|
||||
|
||||
private getTargetUri(req) {
|
||||
const refererURL = new URL(req?.headers?.referer);
|
||||
|
||||
return refererURL.searchParams.get('uri') || '/';
|
||||
}
|
||||
|
||||
private getInvalidPasswordUrl(targetUri: string) {
|
||||
const params = new URLSearchParams();
|
||||
params.append('uri', targetUri);
|
||||
params.append('invalid', 'true');
|
||||
|
||||
const invalidPasswordURI = '/login'.concat('?', params.toString());
|
||||
|
||||
return invalidPasswordURI;
|
||||
}
|
||||
|
||||
@Post('/signin')
|
||||
async login(@Req() req: FastifyRequest, @Res() reply: FastifyReply) {
|
||||
const targetUri = this.getTargetUri(req);
|
||||
const { login, password } = req.body as Credentials;
|
||||
|
||||
try {
|
||||
const token = await this.authService.login(login, password);
|
||||
|
||||
return await reply
|
||||
.setCookie(COOKIE_TOKEN_NAME, token, this.cookieOptions)
|
||||
.status(302)
|
||||
.redirect(targetUri);
|
||||
return await reply.setCookie(COOKIE_TOKEN_NAME, token, this.cookieOptions).status(200).send();
|
||||
} catch {
|
||||
const invalidPasswordURI = this.getInvalidPasswordUrl(targetUri);
|
||||
|
||||
return reply.status(302).redirect(decodeURIComponent(invalidPasswordURI));
|
||||
throw new HttpException('Unauthorized', HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -5,16 +5,36 @@ import { H3 } from 'elements/H';
|
||||
import { useRouter } from 'next/router';
|
||||
import Error from 'elements/Error';
|
||||
import getConfig from 'next/config';
|
||||
import axios from 'axios';
|
||||
import { useState } from 'react';
|
||||
|
||||
const { publicRuntimeConfig: config } = getConfig();
|
||||
|
||||
export default function Form() {
|
||||
const router = useRouter();
|
||||
const { invalid } = router.query;
|
||||
const error = invalid ? <Error>Неверный логин или пароль</Error> : null;
|
||||
const [hasError, setError] = useState(false);
|
||||
const error = hasError ? <Error>Неверный логин или пароль</Error> : null;
|
||||
|
||||
return (
|
||||
<form className={styles.form} method="post" action="/signin">
|
||||
<form
|
||||
className={styles.form}
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
|
||||
const login = e.target[0].value;
|
||||
const password = e.target[1].value;
|
||||
const data = JSON.stringify({ login, password });
|
||||
|
||||
axios
|
||||
.post('/signin', data)
|
||||
.then(() => {
|
||||
router.reload();
|
||||
})
|
||||
.catch(() => {
|
||||
setError(true);
|
||||
});
|
||||
}}
|
||||
>
|
||||
<H3>{config.appTitle}</H3>
|
||||
<Input name="login" type="text" placeholder="Логин" required autoComplete="on" />
|
||||
<Input name="password" type="password" placeholder="Пароль" required autoComplete="on" />
|
||||
|
||||
@ -13,6 +13,7 @@
|
||||
"@types/node": "18.11.9",
|
||||
"@types/react": "18.0.25",
|
||||
"@types/react-dom": "18.0.9",
|
||||
"axios": "^1.2.1",
|
||||
"eslint": "8.28.0",
|
||||
"eslint-config-next": "13.0.5",
|
||||
"next": "13.0.5",
|
||||
|
||||
19
yarn.lock
19
yarn.lock
@ -1728,6 +1728,15 @@ axe-core@^4.4.3:
|
||||
resolved "https://registry.yarnpkg.com/axe-core/-/axe-core-4.5.2.tgz#823fdf491ff717ac3c58a52631d4206930c1d9f7"
|
||||
integrity sha512-u2MVsXfew5HBvjsczCv+xlwdNnB1oQR9HlAcsejZttNjKKSkeDNVwB1vMThIUIFI9GoT57Vtk8iQLwqOfAkboA==
|
||||
|
||||
axios@^1.2.1:
|
||||
version "1.2.1"
|
||||
resolved "https://registry.yarnpkg.com/axios/-/axios-1.2.1.tgz#44cf04a3c9f0c2252ebd85975361c026cb9f864a"
|
||||
integrity sha512-I88cFiGu9ryt/tfVEi4kX2SITsvDddTajXTOFmt2uK1ZVA8LytjtdeyefdQWEf5PU8w+4SSJDoYnggflB5tW4A==
|
||||
dependencies:
|
||||
follow-redirects "^1.15.0"
|
||||
form-data "^4.0.0"
|
||||
proxy-from-env "^1.1.0"
|
||||
|
||||
axobject-query@^2.2.0:
|
||||
version "2.2.0"
|
||||
resolved "https://registry.yarnpkg.com/axobject-query/-/axobject-query-2.2.0.tgz#943d47e10c0b704aa42275e20edf3722648989be"
|
||||
@ -3101,6 +3110,11 @@ flatted@^3.1.0:
|
||||
resolved "https://registry.yarnpkg.com/flatted/-/flatted-3.2.7.tgz#609f39207cb614b89d0765b477cb2d437fbf9787"
|
||||
integrity sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ==
|
||||
|
||||
follow-redirects@^1.15.0:
|
||||
version "1.15.2"
|
||||
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.2.tgz#b460864144ba63f2681096f274c4e57026da2c13"
|
||||
integrity sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==
|
||||
|
||||
fork-ts-checker-webpack-plugin@7.2.13:
|
||||
version "7.2.13"
|
||||
resolved "https://registry.yarnpkg.com/fork-ts-checker-webpack-plugin/-/fork-ts-checker-webpack-plugin-7.2.13.tgz#51ffd6a2f96f03ab64b92f8aedf305dbf3dee0f1"
|
||||
@ -5098,6 +5112,11 @@ proxy-addr@^2.0.7, proxy-addr@~2.0.7:
|
||||
forwarded "0.2.0"
|
||||
ipaddr.js "1.9.1"
|
||||
|
||||
proxy-from-env@^1.1.0:
|
||||
version "1.1.0"
|
||||
resolved "https://registry.yarnpkg.com/proxy-from-env/-/proxy-from-env-1.1.0.tgz#e102f16ca355424865755d2c9e8ea4f24d58c3e2"
|
||||
integrity sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==
|
||||
|
||||
pump@^3.0.0:
|
||||
version "3.0.0"
|
||||
resolved "https://registry.yarnpkg.com/pump/-/pump-3.0.0.tgz#b4a2116815bde2f4e1ea602354e8c75565107a64"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user