Skip to content

Commit

Permalink
ar(temp) auth stuff messy
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloreale committed Jul 13, 2024
1 parent 6f6f318 commit 700ae58
Show file tree
Hide file tree
Showing 16 changed files with 50 additions and 217 deletions.
43 changes: 27 additions & 16 deletions lib/auth/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,37 @@ export const providers = [
const methods = {
signIn: () => {},
signOut: async () => {
console.log('start signout');
const response = await fetch(`${process.env.NEXT_PUBLIC_NEXUS_HOST}/api/auth/signout`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({ csrfToken: await methods.getCsrf() }),
});
return response;
try {
const response = await fetch(`${process.env.NEXT_PUBLIC_NEXUS_HOST}/api/auth/signout`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({ csrfToken: await methods.getCsrf() }),
});
return response;
} catch (e) {
console.error(e);
}
},
getCsrf: async () => {
const response = await fetch(`${process.env.NEXT_PUBLIC_NEXUS_HOST}/api/auth/csrf`);
const csrf = await response.json();
return csrf.csrfToken;
try {
const response = await fetch(`${process.env.NEXT_PUBLIC_NEXUS_HOST}/api/auth/csrf`);
const csrf = await response.json();
return csrf.csrfToken;
} catch (e) {
console.error(e);
}
},
getSession: async () => {
const response = await fetch(`${process.env.NEXT_PUBLIC_NEXUS_HOST}/api/auth/session`);
const session = await response.json();
return session;
try {
const response = await fetch(`${process.env.NEXT_PUBLIC_NEXUS_HOST}/api/auth/session`);
const session = await response.json();
return session;
} catch (e) {
console.error(e);
}
},
};

Expand Down
5 changes: 2 additions & 3 deletions lib/model/decorators/hypnos-public-decorator.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/* eslint @typescript-eslint/consistent-type-assertions:0, @typescript-eslint/no-unused-vars:0 */
// hypnos-public-decorator.ts input: hypnos meta; output: decorated hypnos meta;
'use server';
import type { UserSchema } from '@types';
import type { ICard } from '@dreampipcom/oneiros';

/* private */
const decorateListing = (listing: Record<string, any>, uMeta: UserSchema): ICard => {
const decorateListing = (listing: Record<string, any>, uMeta: any): ICard => {
const decd: ICard = {
id: `list__card--${listing?.title?.es}`,
className: '',
Expand All @@ -29,6 +28,6 @@ const decorateListing = (listing: Record<string, any>, uMeta: UserSchema): ICard
/* public */
export const decorateHypnosPublicListings = async (listings: Record<string, any>[], uid: string): Promise<ICard[]> => {
// const uMeta: UserSchema = await getUserMeta({ email: uid });
const decd: ICard[] = listings?.map((card) => decorateListing(card, { email: uid } as UserSchema));
const decd: ICard[] = listings?.map((card) => decorateListing(card, { email: uid }));
return decd;
};
6 changes: 3 additions & 3 deletions lib/model/decorators/rm-decorator.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint @typescript-eslint/consistent-type-assertions:0 */
// rm-decorator.ts input: rm meta; output: decorated rm meta;
'use server';
import type { IDCharacter, INCharacter, UserSchema } from '@types';
import type { IDCharacter, INCharacter } from '@types';

/* private */
const decorateCharacter = (character: INCharacter, uMeta: UserSchema): IDCharacter => {
const decorateCharacter = (character: INCharacter, uMeta: any): IDCharacter => {
const decd: IDCharacter = { ...character };
decd.favorite = undefined;
if (uMeta?.rickmorty?.favorites?.characters?.includes(character?.id)) decd.favorite = true;
Expand All @@ -15,6 +15,6 @@ const decorateCharacter = (character: INCharacter, uMeta: UserSchema): IDCharact
/* public */
export const decorateRMCharacters = async (characters: INCharacter[], uid: string): Promise<IDCharacter[]> => {
// const uMeta: UserSchema = await getUserMeta({ email: uid });
const decd: IDCharacter[] = characters.map((char) => decorateCharacter(char, { email: uid } as UserSchema));
const decd: IDCharacter[] = characters.map((char) => decorateCharacter(char, { email: uid }));
return decd;
};
3 changes: 1 addition & 2 deletions src/app/components/client/blocks/topnav-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ interface IAuthProvider {
}

interface VTopNavProps {
providers: IAuthProvider[];
user?: UserSchema;
user?: any;
}

export const VTopNav = ({ user }: VTopNavProps) => {
Expand Down
23 changes: 3 additions & 20 deletions src/app/components/client/elements/signin-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import { useContext, useEffect, useRef } from 'react';
import { signOut } from '@auth';
import { AuthContext, GlobalContext } from '@state';
import { ALogIn, ALogOut } from '@actions';
import { ALogOut } from '@actions';
import { navigate } from '@gateway';
import { UserSchema } from '@types';
import { Button as DPButton } from '@dreampipcom/oneiros';

interface IAuthProvider {
Expand All @@ -15,46 +14,30 @@ interface IAuthProvider {

interface VSignInProps {
className?: string;
providers: IAuthProvider[];
user?: UserSchema;
user?: any;
}

async function doSignOut() {
await signOut();
location.reload()
}

export const VSignIn = ({ providers, user }: VSignInProps) => {
export const VSignIn = ({ user }: VSignInProps) => {
const authContext = useContext(AuthContext);
const { authd, name } = authContext;

const globalContext = useContext(GlobalContext);
const { theme } = globalContext;

const [isUserLoaded, loadUser] = ALogIn({});
const [, unloadUser] = ALogOut({});
const initd = useRef(false);


useEffect(() => {
if (!isUserLoaded && user && !initd.current) {
loadUser({
authd: true,
name: user.name,
avatar: user.image,
email: user.email,
});
initd.current = true;
}
}, [isUserLoaded, loadUser]);

const handleSignOut = async () => {
unloadUser();
await doSignOut();
};

// if (!providers) return;

if (user || authd)
return (
<div>
Expand Down
16 changes: 7 additions & 9 deletions src/app/components/client/elements/signup-view.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// signup-view.ts
'use client';
import { signIn, signOut, getCsrf } from "@auth";
import { clsx } from "clsx";
import { useContext, useEffect, useRef, useState } from 'react';
import { signIn, signOut, getCsrf } from "@auth";
import { AuthContext } from '@state';
import { ALogIn, ALogOut } from '@actions';
import { navigate } from '@gateway';
import { UserSchema } from '@types';
import { Button, TextInput } from "@dreampipcom/oneiros";
import { clsx } from "clsx";
import { Logo, Typography } from '@dreampipcom/oneiros';
import { Button, TextInput, Logo, Typography } from "@dreampipcom/oneiros";

interface IAuthProvider {
id?: string;
Expand All @@ -19,7 +17,7 @@ interface IAuthProvider {

interface VSignUpProps {
providers: IAuthProvider[];
user?: UserSchema;
user?: any;
csrf?: string;
}

Expand All @@ -28,8 +26,8 @@ async function doSignOut() {
location.reload();
}

async function doSignIn(id?: string, value?: SignInOptions) {
await signIn(id, value);
async function doSignIn() {
await signIn();
}


Expand Down Expand Up @@ -68,7 +66,7 @@ export const VSignUp = ({ providers, user }: VSignUpProps) => {
}
}, [isUserLoaded, loadUser]);

const handleSignIn = async (id?: string, value?: SignInOptions) => {
const handleSignIn = async () => {
// location.reload()
};

Expand Down
4 changes: 2 additions & 2 deletions src/app/components/server/navbar-controller.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// navbar-controller.tsx
'use server';
import type { IFeature, UserSchema } from '@types';
import type { IFeature } from '@types';
import { getUser } from '@gateway';
import { VNavBar } from '@components/client';

interface INavBarData {
user: UserSchema;
user: any;
}

async function getEnabledFeatures(): Promise<INavBarData> {
Expand Down
9 changes: 0 additions & 9 deletions src/app/components/server/signup-controller.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,7 @@ interface IAuthProviders {
name?: string;
}

async function getProvidersData(): Promise<ISignInData> {
const providers = (await getProviders()) as unknown as IAuthProviders[];
return { providers: providers ?? [] };
}

export const CSignUp = async ({ user }: ISignInProps) => {
const csrf = await getCsrf();
const props: ISignInData = await getProvidersData()
const providers: IAuthProviders[] = props?.providers || []


return <VSignUp providers={providers} csrf={csrf} />
}
2 changes: 1 addition & 1 deletion src/app/components/server/usersettings-controller.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getUser } from '@gateway';
import { VUserSettings } from '@components/client';

interface INavBarData {
user: UserSchema;
user: any;
}

async function getEnabledFeatures(): Promise<INavBarData> {
Expand Down
1 change: 0 additions & 1 deletion src/app/gateway/client/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
import { redirect } from 'next/navigation';

export async function navigate(url: string) {
console.log('redirecting');
redirect(url);
}
4 changes: 2 additions & 2 deletions src/app/gateway/server/actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint @typescript-eslint/consistent-type-assertions:0 */
// actions.ts
'use server';
import type { UserSchema } from '@types';
// import type { UserSchema } from '@types';
import { getRMCharacters } from '@controller';
import { decorateRMCharacters } from '@model';
import { getSession } from '@auth';
Expand Down Expand Up @@ -38,7 +38,7 @@ export async function getUser() {
// const email = session?.user?.email || '';
// const user = await getUserMeta(email);

return { user: { email: 'lorem' } as UserSchema };
return { user: { email: 'lorem' } };
// we might need to decorate users in the future,
// reference decorateRMCharactes()
}
4 changes: 2 additions & 2 deletions src/app/gateway/server/hypnos/public/actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint @typescript-eslint/consistent-type-assertions:0 */
// hypnos/public/actions.ts
'use server';
import type { UserSchema } from '@types';
// import type { UserSchema } from '@types';
import { getHypnosPublicListings } from '@controller';
import { decorateHypnosPublicListings } from '@model';
import { getSession } from '@auth';
Expand Down Expand Up @@ -37,7 +37,7 @@ export async function getUser() {
// const email = session?.user?.email || '';
// const user = await getUserMeta(email);

return { user: { email: 'lorem' } as UserSchema };
return { user: { email: 'lorem' } as any };
// we might need to decorate users in the future,
// reference decorateRMCharactes()
}
8 changes: 0 additions & 8 deletions src/app/styles/components/icons.module.css

This file was deleted.

18 changes: 0 additions & 18 deletions src/app/styles/components/navbar.module.css

This file was deleted.

Loading

0 comments on commit 700ae58

Please sign in to comment.