Skip to content

Commit

Permalink
integrating with lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
voynow committed Sep 2, 2024
1 parent bf43a4d commit 53a046d
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 35 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/deploy_lambda.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ name: Deploy to Lambda

on:
push:
branches:
- main

jobs:
deploy:
Expand Down
1 change: 1 addition & 0 deletions src/lambda_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ def lambda_handler(event, context):
code=event["code"],
)
logging.info(response)
return {"success": True}

elif event.get("end_to_end_test"):
user = get_user(os.environ["JAMIES_ATHLETE_ID"])
Expand Down
6 changes: 4 additions & 2 deletions web/src/app/components/SignUp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ export default function SignUp({ onClose }: SignUpProps): JSX.Element {
const [email, setEmail] = useState('');
const [preferences, setPreferences] = useState('');

const handleSignUp = (event: React.FormEvent) => {
const handleSignUp = (event: React.FormEvent): void => {
event.preventDefault();
localStorage.setItem('email', email);
localStorage.setItem('preferences', preferences);
const stravaAuthUrl = `https://www.strava.com/oauth/authorize?client_id=95101&redirect_uri=http://localhost:3000/verify&response_type=code&approval_prompt=auto&scope=read_all,profile:read_all,activity:read_all`;
const redirectUri = 'http://localhost:3000/';
const stravaAuthUrl = `https://www.strava.com/oauth/authorize?client_id=95101&redirect_uri=${encodeURIComponent(redirectUri)}&response_type=code&approval_prompt=auto&scope=read_all,profile:read_all,activity:read_all`;
window.location.href = stravaAuthUrl;
};


return (
<div className="fixed inset-0 bg-black/25 flex items-center justify-center z-50">
<div className="bg-gray-100 p-8 rounded-lg shadow-2xl max-w-md w-full text-gray-700">
Expand Down
15 changes: 14 additions & 1 deletion web/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
'use client';

import Image from 'next/image';
import { useRouter } from 'next/navigation';
import { useEffect } from 'react';
import Footer from './components/Footer';
import Navbar from './components/Navbar';

export default function Home(): JSX.Element {
const router = useRouter();

useEffect(() => {
const params = new URLSearchParams(window.location.search);
const code = params.get('code');

if (code) {
router.push(`/verify?code=${code}`);
}
}, [router]);

return (
<>
<Navbar />
Expand Down Expand Up @@ -39,4 +52,4 @@ export default function Home(): JSX.Element {
</main>
</>
);
}
}
83 changes: 53 additions & 30 deletions web/src/app/verify/page.tsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,67 @@
'use client';

import { useRouter, useSearchParams } from 'next/navigation';
import { useEffect, useState } from 'react';
import { useEffect, useRef, useState } from 'react';

export default function Verify() {
export default function Verify(): JSX.Element {
const [status, setStatus] = useState<'verifying' | 'success' | 'error'>('verifying');
const router = useRouter();
const { push } = router;
const searchParams = useSearchParams();
const code = searchParams.get('code');
const isInitialMount = useRef(true);

useEffect(() => {
const code = searchParams.get('code');
const email = localStorage.getItem('email');
const preferences = localStorage.getItem('preferences');
if (isInitialMount.current) {
isInitialMount.current = false; // Prevent re-triggering after the first render

if (code && email && preferences) {
fetch('https://your-api-endpoint.com/signup', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ code, email, preferences }),
})
.then(response => response.json())
.then(data => {
if (data.success) {
setStatus('success');
localStorage.removeItem('email');
localStorage.removeItem('preferences');
setTimeout(() => router.push('/'), 2000);
} else {
setStatus('error');
}
const email = localStorage.getItem('email');
const preferences = localStorage.getItem('preferences');

console.log('useEffect triggered');
console.log('Retrieved email:', email);
console.log('Retrieved preferences:', preferences);
console.log('Retrieved code:', code);

if (code && email && preferences) {
console.log('All required data is present. Proceeding with fetch request.');
fetch('https://lwg77yq7dd.execute-api.us-east-1.amazonaws.com/prod/signup', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ email, preferences, code })
})
.catch(() => {
setStatus('error');
});
} else {
setStatus('error');
.then(response => {
console.log('Fetch response received:', response);
return response.json();
})
.then(data => {
console.log('Parsed response data:', data);
if (data.success) {
console.log('Verification successful');
setStatus('success');
localStorage.removeItem('email');
localStorage.removeItem('preferences');
setTimeout(() => {
console.log('Redirecting to home page');
push('/');
}, 2000);
} else {
console.log('Verification failed');
setStatus('error');
}
})
.catch(error => {
console.error('Fetch error:', error);
setStatus('error');
});
} else {
console.log('Missing required data. Setting status to error.');
setStatus('error');
}
}
}, [searchParams, router]);
}, [code, push]); // depend on `code` to ensure updates on URL changes

return (
<div className="flex items-center justify-center min-h-screen bg-gray-100">
Expand All @@ -49,4 +72,4 @@ export default function Verify() {
</div>
</div>
);
}
}

0 comments on commit 53a046d

Please sign in to comment.