feat: add login redirect to the page which initiated auth

This commit is contained in:
Kentai Radiquum 2024-08-04 14:33:22 +05:00
parent d28011b4fb
commit 3378d5b99e
Signed by: Radiquum
GPG key ID: 858E8EE696525EED
7 changed files with 12 additions and 14 deletions

View file

@ -37,11 +37,7 @@
### Авторизация
- [ ] Перенаправление на страницу с которой был запрос на авторизацию
или
- [ ] Использование Modal вместо отдельной страницы
- [X] Перенаправление на страницу с которой был запрос на авторизацию после успешной авторизации
### Рекомендации (Обзор)

View file

@ -194,7 +194,7 @@ export const Navbar = () => {
>
<Dropdown.Item className="text-sm md:text-base">
<Link
href="/login"
href={pathname != "/login" ? `/login?redirect=${pathname}` : "#"}
className="flex items-center gap-1"
>
<span

View file

@ -34,7 +34,7 @@ export function BookmarksPage() {
useEffect(() => {
if (authState === "finished" && !token) {
router.push("/login");
router.push("/login?redirect=/bookmarks");
}
}, [authState, token]);

View file

@ -74,7 +74,7 @@ export function BookmarksCategoryPage(props: any) {
useEffect(() => {
if (authState === "finished" && !token) {
router.push("/login");
router.push(`/login?redirect=/bookmarks/${props.slug}`);
}
}, [authState, token]);

View file

@ -71,7 +71,7 @@ export function FavoritesPage() {
useEffect(() => {
if (authState === "finished" && !token) {
router.push("/login");
router.push("/login?redirect=/favorites");
}
}, [authState, token]);

View file

@ -63,7 +63,7 @@ export function HistoryPage() {
useEffect(() => {
if (authState === "finished" && !token) {
router.push("/login");
router.push("/login?redirect=/history");
}
}, [authState, token]);

View file

@ -2,14 +2,16 @@
import { useState, useEffect } from "react";
import { useUserStore } from "#/store/auth";
import { setJWT } from "#/api/utils";
import { useRouter } from "next/navigation";
import { useRouter, useSearchParams } from "next/navigation";
export function LoginPage() {
const [login, setLogin] = useState("");
const [password, setPassword] = useState("");
const [remember, setRemember]: any = useState(false);
const [remember, setRemember] = useState(true);
const userStore = useUserStore();
const router = useRouter();
const searchParams = useSearchParams();
const redirect = searchParams.get("redirect") || null;
function submit(e) {
e.preventDefault();
@ -45,7 +47,7 @@ export function LoginPage() {
useEffect(() => {
if (userStore.user) {
router.push("/");
router.push(`${redirect || "/"}`);
}
}, [userStore.user]);
@ -108,7 +110,7 @@ export function LoginPage() {
type="checkbox"
className="w-4 h-4 border border-gray-300 rounded bg-gray-50 focus:ring-3 focus:ring-primary-300 dark:bg-gray-700 dark:border-gray-600 dark:focus:ring-primary-600 dark:ring-offset-gray-800"
required={true}
value={remember}
checked={remember}
onChange={(e) => setRemember(e.target.checked)}
/>
</div>