'Next js getServerSideProps redirect with props
Can I get the props after redirection?
return {
redirect: {
destination: '/auth-page',
permanent: false,
},
props: {
initialReduxState: {user},
}
}
After the redirect, I am trying to get the props on the "auth-page". The props object is empty.
Solution 1:[1]
actually i have faced same problem with passing the props from page 1 SSR , to page 2 , i finally i found the the following solution by pass a query params .
and here is the simple example :
page1 : inside "server side props" function we will pass "force_logout" for example :
export async function getServerSideProps(context) { return { redirect: { permanent: false, destination: "/users/login_page/?force_logout=true", }, }; }
page2 : we will receive the query parameter inside "server side props" and send it as props to the same page, as the following :
const loginPage = (props) => { const {query} = props ?? null useEffect(() => { if(query?.force_logout === 'true'){ console.log('yes force_logout is received ') const isServerSide = typeof window === "undefined"; if(!isServerSide){ localStorage.removeItem('test_key') } } }, []) return( <div> test login page </div> ) } export default loginPage export async function getServerSideProps(context) { const {query} = context console.log(query) return { props: {query} } }
this is just example , and change it based on you case/requirements .
i hope this helpful .
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |