Skip to content

Commit

Permalink
Merge pull request #18 from Leanstix/master
Browse files Browse the repository at this point in the history
testing
  • Loading branch information
Leanstix authored Sep 10, 2024
2 parents d5b169a + a234e73 commit 9c1e10a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
33 changes: 19 additions & 14 deletions app/callback/auth/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,30 @@ export default function ValidateToken() {
useEffect(() => {
const handleValidation = async () => {
if (token) {
// Save the token as a cookie to be used later
setCookie("authToken", token, 7); // Cookie expires in 7 days
// Construct the URL to retrieve the user token
const userTokenUrl = `https://api.analogueshifts.app/api/app/callback/${token}`;

try {
// Mock validation process
const isValid = true; // Replace with actual validation logic
// Fetch the user token
const response = await fetch(userTokenUrl);
if (!response.ok) throw new Error("Failed to fetch user token");

const data = await response.json();
const userToken = data?.userToken; // Assuming the response contains a 'userToken'

if (isValid) {
alert("Token validated successfully!"); // Notify the user of success
// Save the user token as a cookie
setCookie("userToken", userToken, 7); // Cookie expires in 7 days

// Notify the user of success
alert("User token retrieved and stored successfully!");

// Redirect the user after validation
setTimeout(() => {
router.push("/"); // Redirect to the homepage or any route
}, 1500); // Delay for user feedback

// Redirect the user after validation
setTimeout(() => {
router.push("/"); // Redirect to the homepage or any route
}, 1500); // Delay for user feedback
} else {
throw new Error("Token validation failed");
}
} catch (error) {
alert("Token validation failed!"); // Notify the user of failure
alert("Failed to retrieve user token!"); // Notify the user of failure
router.push("/"); // Redirect to the homepage or any route
}
} else {
Expand Down
43 changes: 21 additions & 22 deletions app/login/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,49 @@ import Cookies from "js-cookie";
import { useRouter } from "next/navigation";

export default function Login() {
const [loading, setLoading] = useState(true); // To display "Verifying..." text while token is being checked
const [loading, setLoading] = useState(true); // To display "Verifying..." text while fetching user data
const [userData, setUserData] = useState(null); // State to store user details
const router = useRouter();
const url = process.env.NEXT_PUBLIC_BACKEND_URL + "/login";

useEffect(() => {
// Check if the token exists in the cookies
const token = Cookies.get("authToken");

if (token) {
// If token exists, authenticate with it
authenticateWithToken(JSON.parse(token));
// If token exists, fetch user data with it
fetchUserData(JSON.parse(token));
} else {
// If no token exists, redirect to external authentication
window.location.href = "https://auth.analogueshifts.app?app=resume";
}
}, []);

const authenticateWithToken = async (tokenData) => {
setLoading(true);
const fetchUserData = async (tokenData) => {
try {
// Use the token directly for authentication
const res = await fetch(url, {
method: "POST",
const res = await fetch("https://api.analogueshifts.app/api/user", {
method: "GET",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${tokenData.token}`, // Pass the token in the Authorization header
},
body: JSON.stringify({
device_token: crypto.randomUUID(),
}),
});

if (!res.ok) throw new Error("Failed to fetch user information");

const data = await res.json();
if (data.success) {
successToast("Login Successful", "Redirecting You to your Dashboard.");
router.push("/my-resumes");
} else {
throw new Error("Token validation failed");
}
setUserData(data); // Store the fetched user details in state

successToast("User Information Retrieved Successfully");
// Redirect to the dashboard or another route if needed
router.push("/my-resumes");

} catch (error) {
// If the token fails, clear the cookie and redirect to the auth page
// Handle any errors by clearing the token and redirecting to auth page
Cookies.remove("authToken");
errorToast(
"Failed To Login",
error?.response?.data?.message || error.message || "Failed To Login"
"Failed To Retrieve User Information",
error.message || "Failed to fetch user data"
);
window.location.href = "https://auth.analogueshifts.app?app=resume";
} finally {
Expand All @@ -76,8 +74,9 @@ export default function Login() {
<ApplicationLogo />
<div className="pt-11 w-full flex flex-col">
<p className="font-medium text-lg text-tremor-content-grayText pb-4">
Verifying...
{loading ? "Verifying..." : `Welcome, ${userData?.name || "User"}`}
</p>
{/* Additional UI to display user information can be added here */}
</div>
</div>
</section>
Expand Down

0 comments on commit 9c1e10a

Please sign in to comment.