Skip to content

Commit

Permalink
enhance auth to rely on backend
Browse files Browse the repository at this point in the history
  • Loading branch information
SirhmVFX committed Aug 26, 2024
1 parent b8b46a5 commit 9e3f08f
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 151 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"email:dev": "email dev --dir \"./src/email/templates\"",
"email:build": "email build --dir \"./src/email/templates\"",
"email:start": "email start",
"typecheck": "tsc --project tsconfig.json --noEmit"
"typecheck": "tsc --project tsconfig.json --noEmit",
"format": "prettier --write \"src/**/*.{js,jsx,ts,tsx,json,css,md}\""
},
"dependencies": {
"@hookform/resolvers": "^3.9.0",
Expand Down
25 changes: 25 additions & 0 deletions src/actions/blog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"use server";

import axios from "axios";

const apiUrl = process.env.API_URL;
const fetchBlogById = async (id: string) => {
try {
const response = await axios.get(`${apiUrl}/api/v1/blogs/${id}`);
return response.data;
} catch (error) {
return {
message:
axios.isAxiosError(error) &&
error.response &&
error.response.data.message
? error.response.data.message
: "Something went wrong",
status_code:
axios.isAxiosError(error) && error.response
? error.response.status
: undefined,
};
}
};
export { fetchBlogById };
61 changes: 0 additions & 61 deletions src/actions/login.ts

This file was deleted.

52 changes: 0 additions & 52 deletions src/actions/socialAuth.ts

This file was deleted.

102 changes: 102 additions & 0 deletions src/actions/userAuth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
"use server";

import axios from "axios";
import * as z from "zod";

import { LoginSchema } from "~/schemas";
import { AuthResponse, ErrorResponse } from "~/types";

const apiUrl = process.env.API_URL;

const credentialsAuth = async (
values: z.infer<typeof LoginSchema>,
): Promise<AuthResponse | ErrorResponse> => {
const validatedFields = LoginSchema.safeParse(values);
if (!validatedFields.success) {
return {
message: "Something went wrong",
status_code: 401,
};
}
const { email, password } = validatedFields.data;
const payload = { email, password };
try {
const response = await axios.post(`${apiUrl}/api/v1/auth/login`, payload);

return {
data: response.data.user,
access_token: response.data.access_token,
};
} catch (error) {
return {
message:
axios.isAxiosError(error) &&
error.response &&
error.response.data.message
? error.response.data.message
: "Something went wrong",
status_code:
axios.isAxiosError(error) && error.response
? error.response.status
: undefined,
};
}
};

const googleAuth = async (
idToken: string,
): Promise<AuthResponse | ErrorResponse> => {
try {
const response = await axios.post(`${apiUrl}/api/v1/auth/google`, {
id_token: idToken,
});

return {
data: response.data.user,
access_token: response.data.access_token,
};
} catch (error) {
return {
message:
axios.isAxiosError(error) &&
error.response &&
error.response.data.message
? error.response.data.message
: "Something went wrong",
status_code:
axios.isAxiosError(error) && error.response
? error.response.status
: undefined,
};
}
};

const twitterAuth = async (
access_token: string,
): Promise<AuthResponse | ErrorResponse> => {
try {
const response = await axios.post(`${apiUrl}/api/v1/auth/twitter`, {
access_token: access_token,
});

return {
data: response.data.user,
access_token: response.data.access_token,
};
} catch (error) {
return {
message:
axios.isAxiosError(error) &&
error.response &&
error.response.data.message
? error.response.data.message
: "Something went wrong",
status_code:
axios.isAxiosError(error) && error.response
? error.response.status
: undefined,
};
}
};

export { credentialsAuth, googleAuth, twitterAuth };
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Image from "next/image";
import Link from "next/link";
import { FC, useEffect, useState } from "react";

import { fetchBlogById } from "~/actions/blog";
import Comment, {
CommentProperties,
} from "~/components/common/comment-component";
Expand Down Expand Up @@ -50,18 +51,15 @@ const BlogDetailsPage: FC<IProperties> = ({ id }) => {
const fetchData = async () => {
try {
setIsLoading(true);
const response = await fetch(
`https://staging.api-csharp.boilerplate.hng.tech/api/v1/blogs/${id}`,
);
const result = await response.json();
setPost(result.data);
const response = await fetchBlogById(id);
setPost(response.data);
} finally {
setIsLoading(false);
}
};

fetchData();
}, []);
}, [id]);

const handleSubmit = () => {
if (newComment.trim()) {
Expand Down
Loading

0 comments on commit 9e3f08f

Please sign in to comment.