| 
									
										
										
										
											2021-10-12 13:11:33 +00:00
										 |  |  | import { GetServerSidePropsContext } from "next"; | 
					
						
							| 
									
										
										
										
											2021-10-08 11:43:48 +00:00
										 |  |  | import { serverSideTranslations } from "next-i18next/serverSideTranslations"; | 
					
						
							| 
									
										
										
										
											2021-09-21 09:29:20 +00:00
										 |  |  | import React from "react"; | 
					
						
							| 
									
										
										
										
											2021-09-22 19:52:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-02 16:53:13 +00:00
										 |  |  | import { getSession } from "@lib/auth"; | 
					
						
							| 
									
										
										
										
											2021-10-08 11:43:48 +00:00
										 |  |  | import { getOrSetUserLocaleFromHeaders } from "@lib/core/i18n/i18n.utils"; | 
					
						
							|  |  |  | import { useLocale } from "@lib/hooks/useLocale"; | 
					
						
							| 
									
										
										
										
											2021-09-21 09:29:20 +00:00
										 |  |  | import prisma from "@lib/prisma"; | 
					
						
							| 
									
										
										
										
											2021-10-12 13:11:33 +00:00
										 |  |  | import { inferSSRProps } from "@lib/types/inferSSRProps"; | 
					
						
							| 
									
										
										
										
											2021-09-22 19:52:38 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-29 21:33:18 +00:00
										 |  |  | import SettingsShell from "@components/SettingsShell"; | 
					
						
							| 
									
										
										
										
											2021-09-22 19:52:38 +00:00
										 |  |  | import Shell from "@components/Shell"; | 
					
						
							| 
									
										
										
										
											2021-09-21 09:29:20 +00:00
										 |  |  | import ChangePasswordSection from "@components/security/ChangePasswordSection"; | 
					
						
							| 
									
										
										
										
											2021-09-22 19:52:38 +00:00
										 |  |  | import TwoFactorAuthSection from "@components/security/TwoFactorAuthSection"; | 
					
						
							| 
									
										
										
										
											2021-09-21 09:29:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-12 13:11:33 +00:00
										 |  |  | export default function Security({ user }: inferSSRProps<typeof getServerSideProps>) { | 
					
						
							|  |  |  |   const { t } = useLocale(); | 
					
						
							| 
									
										
										
										
											2021-09-21 09:29:20 +00:00
										 |  |  |   return ( | 
					
						
							| 
									
										
										
										
											2021-10-08 11:43:48 +00:00
										 |  |  |     <Shell heading={t("security")} subtitle={t("manage_account_security")}> | 
					
						
							| 
									
										
										
										
											2021-09-21 09:29:20 +00:00
										 |  |  |       <SettingsShell> | 
					
						
							| 
									
										
										
										
											2021-10-12 13:11:33 +00:00
										 |  |  |         <ChangePasswordSection /> | 
					
						
							|  |  |  |         <TwoFactorAuthSection twoFactorEnabled={user.twoFactorEnabled} /> | 
					
						
							| 
									
										
										
										
											2021-09-21 09:29:20 +00:00
										 |  |  |       </SettingsShell> | 
					
						
							|  |  |  |     </Shell> | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-12 13:11:33 +00:00
										 |  |  | export async function getServerSideProps(context: GetServerSidePropsContext) { | 
					
						
							| 
									
										
										
										
											2021-09-21 09:29:20 +00:00
										 |  |  |   const session = await getSession(context); | 
					
						
							| 
									
										
										
										
											2021-10-08 11:43:48 +00:00
										 |  |  |   const locale = await getOrSetUserLocaleFromHeaders(context.req); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-02 16:53:13 +00:00
										 |  |  |   if (!session?.user?.id) { | 
					
						
							| 
									
										
										
										
											2021-09-21 09:29:20 +00:00
										 |  |  |     return { redirect: { permanent: false, destination: "/auth/login" } }; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const user = await prisma.user.findFirst({ | 
					
						
							|  |  |  |     where: { | 
					
						
							| 
									
										
										
										
											2021-10-02 16:53:13 +00:00
										 |  |  |       id: session.user.id, | 
					
						
							| 
									
										
										
										
											2021-09-21 09:29:20 +00:00
										 |  |  |     }, | 
					
						
							|  |  |  |     select: { | 
					
						
							|  |  |  |       id: true, | 
					
						
							|  |  |  |       username: true, | 
					
						
							|  |  |  |       name: true, | 
					
						
							|  |  |  |       twoFactorEnabled: true, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-12 13:11:33 +00:00
										 |  |  |   if (!user) { | 
					
						
							|  |  |  |     return { redirect: { permanent: false, destination: "/auth/login" } }; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-21 09:29:20 +00:00
										 |  |  |   return { | 
					
						
							| 
									
										
										
										
											2021-10-08 11:43:48 +00:00
										 |  |  |     props: { | 
					
						
							|  |  |  |       session, | 
					
						
							|  |  |  |       user, | 
					
						
							|  |  |  |       ...(await serverSideTranslations(locale, ["common"])), | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2021-09-21 09:29:20 +00:00
										 |  |  |   }; | 
					
						
							|  |  |  | } |