diff --git a/ao3 webpages/components/Login.jsx b/ao3 webpages/components/Login.jsx index 224b7c7..6bbf87b 100644 --- a/ao3 webpages/components/Login.jsx +++ b/ao3 webpages/components/Login.jsx @@ -1,62 +1,58 @@ -import React, { useState,useEffect } from 'react'; +import React, { useState, useEffect } from 'react'; import "../css/Login.css"; export default function Login() { const [email, setEmail] = useState(''); - const [password, setPassword] = useState(''); + + const [password, setPassword] + = useState(''); const [message, setMessage] = useState(''); const [messageType, setMessageType] = useState(''); + const [showPassword, setShowPassword] = useState(false); const togglePasswordVisibility = () => { setShowPassword(!showPassword); }; - useEffect(() => { + + useEffect(()=> { // Check for existing tokens in localStorage const accessToken = localStorage.getItem('accessToken'); const refreshToken = localStorage.getItem('refreshToken'); + console.log('Checking tokens:', { accessToken, refreshToken }); + if (accessToken && refreshToken) { - // Redirect to recommendations page if tokens are found - // window.location.href = '/dashboard'; - const checkAuth = async () => { - try { - const response = await fetch('https://ao3-chrome-extension-backend.onrender.com/auth/validate', { + // Validate tokens before redirecting + const checkAuth = async () => { + try { + const response = await fetch('https://ao3-chrome-extension-backend.onrender.com/auth/validate', { method: 'GET', headers: { - 'Content-Type': 'application/json', - 'Tokens': JSON.stringify({ accessToken, refreshToken }), + 'Content-Type': 'application/json', + 'Tokens': JSON.stringify({ accessToken, refreshToken }), }, - }); + }); - if (response.ok) { - window.location.href = '/dashboard'; - } else { + if (response.ok) { + window.location.href = '/dashboard'; + } else { // Tokens are invalid console.log('Token validation failed:', response.status); - alert("Token validation failed"); + alert('Token validation failed'); + } + } catch (error) { + console.error('Error during token validation:', error); } - } catch (error) { - console.error('Error during token validation:', error); - - } - } - checkAuth(); + }; + + checkAuth(); } - } - , []); - const dispatchTokenEvent = (accessToken, refreshToken) => { - const event = new CustomEvent("AO3_SEND_TOKENS", { - - detail: { accessToken, refreshToken } - - }); - window.dispatchEvent(event); - }; + }, []); const handleSubmit = async (e) => { e.preventDefault(); - + try { const response = await fetch('https://ao3-chrome-extension-backend.onrender.com/auth/login', { method: 'POST', @@ -64,26 +60,28 @@ export default function Login() { 'Content-Type': 'application/x-www-form-urlencoded', }, body: new URLSearchParams({ - email: email, - password: password, + email, + password, }), }); - + const data = await response.json(); console.log('Response:', response); console.log('Response data:', data); - + if (response.ok) { // Store tokens in localStorage localStorage.setItem('accessToken', data.accessToken); - localStorage.setItem('refreshToken', data.refreshToken); - - // Dispatch event to send tokens to the extension - dispatchTokenEvent(data.accessToken, data.refreshToken); - + localStorage.setItem('refreshToken', data.refreshToken); + + // Send token to Chrome extension + chrome.runtime.sendMessage("nnmmeljlhmhpnfphcpifdahblfmhlilm", { action: "storeToken", token: data.refreshToken }, function(response) { + console.log('Token sent to extension'); + }); + setMessage('Login successful'); setMessageType('success'); - + setTimeout(() => { window.location.href = '/dashboard'; }, 1500); @@ -97,14 +95,15 @@ export default function Login() { setMessageType('error'); } }; + - return ( -
- Log in to access your personalized AO3 tools. Continue your fanfiction journey right where you left off. -
- -+ Log in to access your personalized AO3 tools. Continue your fanfiction journey right where you left off. +
+ ++ Don't have an account? Signup +