Skip to content

Commit

Permalink
feat: ✨ add login redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
lucca180 committed Nov 17, 2024
1 parent e482adf commit 734c411
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 24 deletions.
6 changes: 3 additions & 3 deletions pages/admin/createItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from '@chakra-ui/react';
import axios from 'axios';
import { getCookie } from 'cookies-next';
import { NextPageContext, NextApiRequest } from 'next';
import { NextApiRequest, GetServerSidePropsContext } from 'next';
import React, { useState } from 'react';
import HeaderCard from '../../components/Card/HeaderCard';
import Layout from '../../components/Layout';
Expand Down Expand Up @@ -264,7 +264,7 @@ const CreateItem = () => {

export default CreateItem;

export async function getServerSideProps(context: NextPageContext) {
export async function getServerSideProps(context: GetServerSidePropsContext) {
try {
const token = getCookie('userToken', { req: context.req, res: context.res }) as
| string
Expand All @@ -285,7 +285,7 @@ export async function getServerSideProps(context: NextPageContext) {
} catch (e) {
return {
redirect: {
destination: '/',
destination: `/login?redirect=${encodeURIComponent(context.resolvedUrl)}`,
permanent: false,
},
};
Expand Down
6 changes: 3 additions & 3 deletions pages/feedback/trades.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
} from '@chakra-ui/react';
import axios from 'axios';
import { getCookie } from 'cookies-next';
import { NextApiRequest, NextPageContext } from 'next';
import { GetServerSidePropsContext, NextApiRequest } from 'next';
import { ReactElement, useEffect, useRef, useState } from 'react';
import { BsXLg, BsXCircleFill, BsCheckCircleFill, BsCheckLg } from 'react-icons/bs';
import CardBase from '../../components/Card/CardBase';
Expand Down Expand Up @@ -344,7 +344,7 @@ export const TradeGuidelines = () => {
);
};

export async function getServerSideProps(context: NextPageContext) {
export async function getServerSideProps(context: GetServerSidePropsContext) {
try {
const token = getCookie('userToken', { req: context.req, res: context.res }) as
| string
Expand All @@ -371,7 +371,7 @@ export async function getServerSideProps(context: NextPageContext) {
} catch (e) {
return {
redirect: {
destination: '/login',
destination: `/login?redirect=${encodeURIComponent(context.resolvedUrl)}`,
permanent: false,
},
};
Expand Down
6 changes: 3 additions & 3 deletions pages/feedback/vote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { Feedback, TradeData } from '../../types';
import { useAuth } from '../../utils/auth';
import { TradeGuidelines } from './trades';
import { getCookie } from 'cookies-next';
import { NextPageContext, NextApiRequest } from 'next';
import { NextApiRequest, GetServerSidePropsContext } from 'next';
import { CheckAuth } from '../../utils/googleCloud';
import { createTranslator, useTranslations } from 'next-intl';
import dynamic from 'next/dynamic';
Expand Down Expand Up @@ -352,7 +352,7 @@ export default FeedbackVotingPage;
// );
// };

export async function getServerSideProps(context: NextPageContext) {
export async function getServerSideProps(context: GetServerSidePropsContext) {
try {
const token = getCookie('userToken', { req: context.req, res: context.res }) as
| string
Expand All @@ -379,7 +379,7 @@ export async function getServerSideProps(context: NextPageContext) {
} catch (e) {
return {
redirect: {
destination: '/login',
destination: `/login?redirect=${encodeURIComponent(context.resolvedUrl)}`,
permanent: false,
},
};
Expand Down
4 changes: 2 additions & 2 deletions pages/lists/[username]/[list_id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import Color from 'color';
import SortableArea from '../../../components/Sortable/SortableArea';
import { SelectItemsCheckbox } from '../../../components/Input/SelectItemsCheckbox';
import { ItemActionModalProps } from '../../../components/Modal/ItemActionModal';
import { NextPageContext } from 'next';
import { GetServerSidePropsContext } from 'next';
import { getList } from '../../api/v1/lists/[username]/[list_id]';
import { getCookie } from 'cookies-next';

Expand Down Expand Up @@ -842,7 +842,7 @@ const ListPage = (props: Props) => {

export default ListPage;

export async function getServerSideProps(context: NextPageContext) {
export async function getServerSideProps(context: GetServerSidePropsContext) {
const { list_id, username } = context.query;
if (!username || !list_id || Array.isArray(username) || Array.isArray(list_id))
return { notFound: true };
Expand Down
12 changes: 6 additions & 6 deletions pages/lists/[username]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { FaTrash } from 'react-icons/fa';
import { DeleteListModalProps } from '../../../components/Modal/DeleteListModal';
import { BiLinkExternal } from 'react-icons/bi';
import { EditProfileModalProps } from '../../../components/Modal/EditProfileModal';
import { NextPageContext } from 'next';
import { GetServerSidePropsContext } from 'next';
import { getUser } from '../../api/v1/users/[username]';
import NextImage from 'next/image';
import icon from '../../../public/logo_icon.svg';
Expand All @@ -40,15 +40,15 @@ import dynamic from 'next/dynamic';
import { useTranslations } from 'next-intl';

const CreateListModal = dynamic<CreateListModalProps>(
() => import('../../../components/Modal/CreateListModal'),
() => import('../../../components/Modal/CreateListModal')
);

const DeleteListModal = dynamic<DeleteListModalProps>(
() => import('../../../components/Modal/DeleteListModal'),
() => import('../../../components/Modal/DeleteListModal')
);

const EditProfileModal = dynamic<EditProfileModalProps>(
() => import('../../../components/Modal/EditProfileModal'),
() => import('../../../components/Modal/EditProfileModal')
);

type ExtendedUserList = UserList & {
Expand Down Expand Up @@ -234,7 +234,7 @@ const UserListsPage = (props: Props) => {
headers: {
authorization: `Bearer ${token}`,
},
},
}
);

if (res.data.success) {
Expand Down Expand Up @@ -508,7 +508,7 @@ const UserListsPage = (props: Props) => {
);
};

export async function getServerSideProps(context: NextPageContext) {
export async function getServerSideProps(context: GetServerSidePropsContext) {
const { username } = context.query;
if (!username || Array.isArray(username)) return { notFound: true };

Expand Down
12 changes: 8 additions & 4 deletions pages/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const LoginPage = () => {
const init = async () => {
setIsLoading(true);
const { auth, isSignInWithEmailLink, signInWithEmailLink } = await getAuth();
const redirectTo = (router.query.redirect as string | undefined) ?? '/';

if (isSignInWithEmailLink(auth, window.location.href) && !user) {
let mailAddr = window.localStorage.getItem('emailForSignIn');
Expand Down Expand Up @@ -77,7 +78,7 @@ const LoginPage = () => {

setUser(user);

router.replace('/');
router.replace(decodeURIComponent(redirectTo));
} catch (e: any) {
setError(e.message);
console.error(error);
Expand All @@ -90,7 +91,7 @@ const LoginPage = () => {
return setNeedInfo(true);
}

router.replace('/');
router.replace(decodeURIComponent(redirectTo));
} else onOpen();
};

Expand All @@ -109,6 +110,8 @@ const LoginPage = () => {
};

const saveChanges = async () => {
const redirectTo = (router.query.redirect as string | undefined) ?? '/';

setError('');
if (!neopetsUser || !username) {
setError(t('Login.please-fill-all-fields'));
Expand Down Expand Up @@ -151,7 +154,7 @@ const LoginPage = () => {

setUser(user);

router.replace('/');
router.replace(decodeURIComponent(redirectTo));
} catch (e: any) {
setError(e.message);
console.error(error);
Expand All @@ -160,7 +163,8 @@ const LoginPage = () => {

const closeLogin = () => {
onClose();
router.replace('/');
const redirectTo = (router.query.redirect as string | undefined) ?? '/';
router.replace(decodeURIComponent(redirectTo));
};

const checkUsername = async (): Promise<boolean> => {
Expand Down
6 changes: 3 additions & 3 deletions pages/tools/data-collecting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { ReactElement, useState } from 'react';
import ItemCard from '../../components/Items/ItemCard';
import axios from 'axios';
import { getCookie } from 'cookies-next';
import { NextPageContext, NextApiRequest } from 'next';
import { NextApiRequest, GetServerSidePropsContext } from 'next';
import { CheckAuth } from '../../utils/googleCloud';

const DATA_COLLECTING_OPTIONS: {
Expand Down Expand Up @@ -183,7 +183,7 @@ const DataCollectingPage = () => {

export default DataCollectingPage;

export async function getServerSideProps(context: NextPageContext) {
export async function getServerSideProps(context: GetServerSidePropsContext) {
try {
const token = getCookie('userToken', { req: context.req, res: context.res }) as
| string
Expand All @@ -210,7 +210,7 @@ export async function getServerSideProps(context: NextPageContext) {
} catch (e) {
return {
redirect: {
destination: '/login',
destination: `/login?redirect=${encodeURIComponent(context.resolvedUrl)}`,
permanent: false,
},
};
Expand Down

0 comments on commit 734c411

Please sign in to comment.