Skip to content

Commit

Permalink
Resolved merge conflicts in src/plugins/test.js
Browse files Browse the repository at this point in the history
  • Loading branch information
goswami34 committed Jan 25, 2025
2 parents 9a27096 + cc83c4c commit 4b32c99
Show file tree
Hide file tree
Showing 11 changed files with 447 additions and 233 deletions.
30 changes: 27 additions & 3 deletions src/context/AuthContext.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import axios from "axios";
import { useState, createContext, useEffect, useMemo, useCallback, Fragment } from "react";

export const AuthContext = createContext();
Expand All @@ -8,7 +9,6 @@ const AuthProvider = ({ children }) => {
const [error, setError] = useState(null);
const [loading, setLoading] = useState(false);

// Memoized helper function to update user state and persist in localStorage
const setUser = useCallback((userData) => {
if (userData) {
localStorage.setItem("squarCraft_user", JSON.stringify(userData));
Expand Down Expand Up @@ -55,13 +55,37 @@ const AuthProvider = ({ children }) => {
}
}, [setUser]);

const logoutUser = useCallback(async () => {
setLoading(true);
try {
const response = await axios.post("http://localhost:8000/api/v1/logout", {}, {
headers: {
Authorization: `Bearer ${localStorage.getItem("squarCraft_auth_token")}`,
},
});
console.log(response.data.message);
setUser(null);
localStorage.removeItem("squarCraft_auth_token");
sessionStorage.removeItem("squarCraft_auth_token");
document.cookie = "squarCraft_auth_token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
document.cookie = "squarCraft_auth_token=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/; domain=.yoursquarespace.com; secure; samesite=strict;";
setError(null);
} catch (err) {
console.error("Logout Error:", err.message);
setError("Failed to log out. Please try again.");
} finally {
setLoading(false);
}
}, [setUser]);

const contextValue = useMemo(() => ({
user,
registerUser,
loginUser,
error,
loading
}), [user, registerUser, loginUser, error, loading]);
loading,
logoutUser,
}), [user, registerUser, loginUser, error, loading,logoutUser]);

return (
<AuthContext.Provider value={contextValue}>
Expand Down
28 changes: 28 additions & 0 deletions src/hooks/AutenticateWrapper/AutenticateWrapper.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { useEffect, useMemo } from "react";

// eslint-disable-next-line react/prop-types
const AuthenticateWrapper = ({ children }) => {
const authenticateRoutes = useMemo(() => [
"/auth/login",
"/auth/register",
"/auth/register-otp",
"/auth/register-success",
"/auth/Forgot-pass-email-verify",
"/auth/Forgot-pass-email-otp",
"/auth/forgot-pass-set-new-pass",
"/auth/forgot-pass-pass-updated",
], []);

useEffect(() => {
const storedUser = localStorage.getItem("squarCraft_user");
const currentPath = window.location.pathname;

if (storedUser && authenticateRoutes.includes(currentPath)) {
window.location.href = "/dashboard/myWebsites";
}
}, [authenticateRoutes]);

return <>{children}</>;
};

export default AuthenticateWrapper;
11 changes: 7 additions & 4 deletions src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import React from 'react';
import ReactDOM from 'react-dom/client';
import { RouterProvider } from 'react-router-dom';
import './index.css';
import { router } from './routes/Routes';
import { router } from './routes/Routes';
import AuthProvider from './context/AuthContext';
import AuthenticateWrapper from './hooks/AutenticateWrapper/AutenticateWrapper';

ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<AuthProvider>
<RouterProvider router={router} />
</AuthProvider>
<AuthProvider>
<AuthenticateWrapper>
<RouterProvider router={router} />
</AuthenticateWrapper>
</AuthProvider>
</React.StrictMode>
);
4 changes: 3 additions & 1 deletion src/pages/Home/Home/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { motion } from "framer-motion";
import { Link } from "react-router-dom";
import sampleVideo from "../../../../public/Statics/sampleVideo.mp4";
const Home = () => {
const storedUser = localStorage.getItem("squarCraft_user");

return (
<div className="bg-white pt-16 pb-20 px-6 md:px-12">
{/* Hero Section */}
Expand All @@ -27,7 +29,7 @@ const Home = () => {
transition={{ duration: 0.6, delay: 0.3 }}
className="mt-6 flex justify-center gap-4"
>
<Link to="/auth/login">
<Link to={`${storedUser ? "/dashboard/myWebsites" : "/auth/login"}`}>
<button className="px-6 py-3 bg-jaffa-400 hover:bg-orange-700 text-white text-lg font-medium rounded-xl transition-all shadow-md">
Get Started
</button>
Expand Down
49 changes: 38 additions & 11 deletions src/pages/auth/Login/Login.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from "react";
import { useContext, useState } from "react";
import axios from "axios";
import { useNavigate } from "react-router-dom";
import { Link } from "react-router-dom";
Expand All @@ -11,9 +11,12 @@ import useTitle from "../../../hooks/useTitle";
import tik from "../../../../public/images/auth/login/tik.svg";
import Notification from "../../../hooks/Notification/Notification";
import ButtonLoader from "../../../hooks/ButtonLoader/ButtonLoader";
import { AuthContext } from "../../../context/AuthContext";

const Login = () => {
useTitle("Sign In | SquareCraft");
const { loginUser} = useContext(AuthContext);

const navigate = useNavigate();
const [email, setEmail] = useState("");
const [password, setPassword] = useState("");
Expand Down Expand Up @@ -41,34 +44,58 @@ const navigate = useNavigate();
const handleSubmit = async (e) => {
e.preventDefault();
if (!validate()) return;

try {
setLoading(true);

// Call the API for login
const response = await axios.post("http://localhost:8000/api/v1/login", {
email,
password,
rememberMe: isChecked,
});

const loginUserData = {
name: response?.data?.user?.name,
email: response?.data?.user?.email,
user_id: response?.data?.user?.id,
squarCraft_auth_token: response?.data?.squarCraft_auth_token,
};

// Use AuthContext to set user
loginUser(loginUserData);

const squarCraft_auth_token = response.data.squarCraft_auth_token;
console.log("squarCraft_auth_token", squarCraft_auth_token)
console.log("squarCraft_auth_token", squarCraft_auth_token);

// Save token locally in your plugin
localStorage.setItem("squarCraft_auth_token", squarCraft_auth_token);
sessionStorage.setItem("squarCraft_auth_token", squarCraft_auth_token);
document.cookie = `squarCraft_auth_token=${squarCraft_auth_token}; path=/; max-age=${60 * 60}`;
document.cookie = `squarCraft_auth_token=${squarCraft_auth_token}; path=/; max-age=${60 * 60 * 24}; domain=.yoursquarespace.com; secure; samesite=strict`;
// Send the squarCraft_auth_token to the Squarespace page
window.parent.postMessage({ type: "squarCraft_auth_token", squarCraft_auth_token: squarCraft_auth_token }, "*");

if(response.status===200){

// Set cookies for Squarespace and its subdomains
const expires = new Date(Date.now() + 60 * 60 * 1000).toUTCString(); // Expires in 1 hour
document.cookie = `squarCraft_auth_token=${squarCraft_auth_token}; path=/; max-age=${60 * 60}; secure; samesite=none`;
document.cookie = `squarCraft_auth_token=${squarCraft_auth_token}; domain=.squarespace.com; path=/; expires=${expires}; secure; samesite=none`;

console.log("Token successfully set for Squarespace cookies:", document.cookie);

// Notify parent domain (Squarespace) about the token
window.parent.postMessage({ type: "squarCraft_auth_token", squarCraft_auth_token }, "*");

// Navigate to dashboard after successful login
if (response.status === 200) {
navigate("/dashboard/myWebsites");
}

console.log("Login successful:", response.data);
} catch (error) {
setErrors({ api: error.response?.data?.message || "An error occurred." });
console.error("Error during login:", error.message);
} finally {
setLoading(false);
}
};




return (
<div className="w-full flex items-center justify-center mt-[6.5rem] xl:mt-[10rem] xl:px-8">
Expand Down
3 changes: 2 additions & 1 deletion src/pages/auth/Register/Register.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ const RegisterSchema = () => {
name:response.data.user.name,
email: response.data.user.email,
squarCraft_auth_token: response.data.squarCraft_auth_token,
user_id: response.data.user.id
user_id: response.data.user.id,
phone: response.data.user.phone || "",
}
console.log("registered user data" , registerUserData)
registerUser(registerUserData);
Expand Down
2 changes: 1 addition & 1 deletion src/pages/dashboard/MyWebsite/MyWebsite.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const MyWebsite = () => {
const { user, loading, error } = useContext(AuthContext);

console.log("squarCraft_auth_token - ", localStorage.getItem("squarCraft_auth_token"))
const installationCode = `<script id="squarecraft-script" src="https://fatin-webefo.github.io/squarecraft-frontend/src/plugins/test.js" token="${user?.user_id}" defer></script>`;
const installationCode = `<script id="squarecraft-script" src="https://fatin-webefo.github.io/squarecraft-frontend/src/plugins/plugin.js" token="${user?.user_id}" defer></script>`;

const [plugins, setPlugins] = useState([{ id: 1, name: "", copied: false }]);

Expand Down
Loading

0 comments on commit 4b32c99

Please sign in to comment.