1
0
mirror of https://github.com/stonith404/pingvin-share.git synced 2024-11-05 15:30:14 +01:00

fix: refresh token gets deleted on session end

This commit is contained in:
Elias Schneider 2022-12-26 12:57:54 +01:00
parent b73144295b
commit e5b50f855c
5 changed files with 23 additions and 23 deletions

View File

@ -1,5 +1,4 @@
import { PickType } from "@nestjs/mapped-types"; import { PickType } from "@nestjs/mapped-types";
import { IsEmail, IsOptional, IsString } from "class-validator";
import { UserDTO } from "src/user/dto/user.dto"; import { UserDTO } from "src/user/dto/user.dto";
export class EnableTotpDTO extends PickType(UserDTO, ["password"] as const) {} export class EnableTotpDTO extends PickType(UserDTO, ["password"] as const) {}

View File

@ -1,5 +1,5 @@
import { PickType } from "@nestjs/mapped-types"; import { PickType } from "@nestjs/mapped-types";
import { IsEmail, IsOptional, IsString } from "class-validator"; import { IsString } from "class-validator";
import { UserDTO } from "src/user/dto/user.dto"; import { UserDTO } from "src/user/dto/user.dto";
export class VerifyTotpDTO extends PickType(UserDTO, ["password"] as const) { export class VerifyTotpDTO extends PickType(UserDTO, ["password"] as const) {

View File

@ -10,7 +10,6 @@ import {
} from "@mantine/core"; } from "@mantine/core";
import { useForm, yupResolver } from "@mantine/form"; import { useForm, yupResolver } from "@mantine/form";
import { showNotification } from "@mantine/notifications"; import { showNotification } from "@mantine/notifications";
import { setCookie } from "cookies-next";
import Link from "next/link"; import Link from "next/link";
import React from "react"; import React from "react";
import { TbInfoCircle } from "react-icons/tb"; import { TbInfoCircle } from "react-icons/tb";
@ -59,8 +58,6 @@ const SignInForm = () => {
}); });
setLoginToken(response.data["loginToken"]); setLoginToken(response.data["loginToken"]);
} else { } else {
setCookie("access_token", response.data.accessToken);
setCookie("refresh_token", response.data.refreshToken);
window.location.replace("/"); window.location.replace("/");
} }
}) })
@ -70,11 +67,7 @@ const SignInForm = () => {
const signInTotp = (email: string, password: string, totp: string) => { const signInTotp = (email: string, password: string, totp: string) => {
authService authService
.signInTotp(email, password, totp, loginToken) .signInTotp(email, password, totp, loginToken)
.then((response) => { .then(() => window.location.replace("/"))
setCookie("access_token", response.data.accessToken);
setCookie("refresh_token", response.data.refreshToken);
window.location.replace("/");
})
.catch((error) => { .catch((error) => {
if (error?.response?.data?.message == "Login token expired") { if (error?.response?.data?.message == "Login token expired") {
toast.error("Login token expired"); toast.error("Login token expired");

View File

@ -9,7 +9,6 @@ import {
Title, Title,
} from "@mantine/core"; } from "@mantine/core";
import { useForm, yupResolver } from "@mantine/form"; import { useForm, yupResolver } from "@mantine/form";
import { setCookie } from "cookies-next";
import Link from "next/link"; import Link from "next/link";
import * as yup from "yup"; import * as yup from "yup";
import useConfig from "../../hooks/config.hook"; import useConfig from "../../hooks/config.hook";
@ -37,11 +36,7 @@ const SignUpForm = () => {
const signUp = (email: string, username: string, password: string) => { const signUp = (email: string, username: string, password: string) => {
authService authService
.signUp(email, username, password) .signUp(email, username, password)
.then((response) => { .then(() => window.location.replace("/"))
setCookie("access_token", response.data.accessToken);
setCookie("refresh_token", response.data.refreshToken);
window.location.replace("/");
})
.catch(toast.axiosError); .catch(toast.axiosError);
}; };

View File

@ -11,6 +11,12 @@ const signIn = async (emailOrUsername: string, password: string) => {
...emailOrUsernameBody, ...emailOrUsernameBody,
password, password,
}); });
setCookie("access_token", response.data.accessToken);
setCookie("refresh_token", response.data.refreshToken, {
maxAge: 60 * 60 * 24 * 30 * 3,
});
return response; return response;
}; };
@ -34,7 +40,14 @@ const signInTotp = async (
}; };
const signUp = async (email: string, username: string, password: string) => { const signUp = async (email: string, username: string, password: string) => {
return await api.post("auth/signUp", { email, username, password }); const response = await api.post("auth/signUp", { email, username, password });
setCookie("access_token", response.data.accessToken);
setCookie("refresh_token", response.data.refreshToken, {
maxAge: 60 * 60 * 24 * 30 * 3,
});
return response;
}; };
const signOut = () => { const signOut = () => {
@ -45,14 +58,14 @@ const signOut = () => {
const refreshAccessToken = async () => { const refreshAccessToken = async () => {
try { try {
const currentAccessToken = getCookie("access_token") as string; const accessToken = getCookie("access_token") as string;
if (
currentAccessToken &&
(jose.decodeJwt(currentAccessToken).exp ?? 0) * 1000 <
Date.now() + 2 * 60 * 1000
) {
const refreshToken = getCookie("refresh_token"); const refreshToken = getCookie("refresh_token");
if (
(accessToken &&
(jose.decodeJwt(accessToken).exp ?? 0) * 1000 <
Date.now() + 2 * 60 * 1000) ||
(refreshToken && !accessToken)
) {
const response = await api.post("auth/token", { refreshToken }); const response = await api.post("auth/token", { refreshToken });
setCookie("access_token", response.data.accessToken); setCookie("access_token", response.data.accessToken);
} }