mocksaml/pages/saml/login.tsx

89 lines
2.7 KiB
TypeScript
Raw Normal View History

2022-02-22 08:18:07 +00:00
import Head from 'next/head';
2022-02-24 06:20:00 +00:00
import { useEffect, useRef, useState } from 'react';
2022-02-22 08:18:07 +00:00
import type { FormEvent } from 'react';
import { useRouter } from 'next/router';
2022-02-18 18:12:45 +00:00
export default function Login() {
2022-02-22 08:18:07 +00:00
const router = useRouter();
const { id, audience, acsUrl, providerName, relayState } = router.query;
2022-02-24 06:20:00 +00:00
const [email, setEmail] = useState('jackson');
// Set focus to email input on load
const emailInp = useRef<HTMLInputElement>(null);
useEffect(() => {
if (emailInp.current) {
emailInp.current.focus();
emailInp.current.select();
}
}, []);
2022-02-22 08:18:07 +00:00
const handleChange = (e: FormEvent<HTMLInputElement>): void => {
setEmail(e.currentTarget.value);
};
const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
const response = await fetch(`/api/saml/auth`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
2022-02-24 08:04:55 +00:00
body: JSON.stringify({ email: `${email}@example.com`, id, audience, acsUrl, providerName, relayState }),
2022-02-22 08:18:07 +00:00
});
if (response.ok) {
2022-02-22 08:33:22 +00:00
const newHtml = await response.text();
const newDoc = document.open('text/html', 'replace');
newDoc.write(newHtml);
newDoc.close();
2022-02-22 08:18:07 +00:00
} else {
document.write('Error in getting SAML response');
}
};
2022-02-18 18:12:45 +00:00
return (
2022-02-22 08:18:07 +00:00
<div className='h-full'>
2022-02-18 18:12:45 +00:00
<Head>
<title>Mock SAML IdP - Login</title>
</Head>
2022-02-28 18:42:44 +00:00
<div className='relative top-20 mx-auto w-[465px] max-w-[90%] rounded-xl p-10 text-[#145698] shadow-lg shadow-indigo-200'>
2022-02-24 18:16:45 +00:00
<h2 className='mb-3 text-center text-3xl font-bold'>Login</h2>
2022-02-22 08:18:07 +00:00
<form onSubmit={handleSubmit}>
2022-02-18 18:12:45 +00:00
<div>
2022-02-24 18:16:45 +00:00
<label htmlFor='email' className='mb-2 block'>
2022-02-18 18:12:45 +00:00
Email
</label>
<input
2022-02-22 08:18:07 +00:00
id='email'
2022-02-24 06:20:40 +00:00
ref={emailInp}
2022-02-22 08:18:07 +00:00
autoComplete='off'
2022-02-24 06:20:40 +00:00
type='text'
placeholder='jackson'
2022-02-22 08:18:07 +00:00
value={email}
onChange={handleChange}
2022-02-24 18:16:45 +00:00
className='input w-[65%]'
2022-02-22 08:18:07 +00:00
title='please provide a mock example.com email address'
2022-02-18 18:12:45 +00:00
/>
2022-02-24 06:20:40 +00:00
<span className='ml-2 w-1/4'>@example.com</span>
2022-02-18 18:12:45 +00:00
</div>
2022-02-22 08:18:07 +00:00
<div className='mt-5'>
2022-02-24 18:16:45 +00:00
<label htmlFor='password' className='mb-2 block'>
2022-02-18 18:12:45 +00:00
Password <sup>(Prefilled for you)</sup>
</label>
<input
2022-02-22 08:18:07 +00:00
id='password'
2022-02-18 18:12:45 +00:00
readOnly={true}
2022-02-22 08:18:07 +00:00
autoComplete='off'
type='password'
defaultValue='samlstrongpassword'
2022-02-24 18:16:45 +00:00
className='input w-full'
2022-02-18 18:12:45 +00:00
/>
</div>
2022-02-22 08:18:07 +00:00
<button type='submit' className='button mt-8 w-full'>
2022-02-18 18:12:45 +00:00
Sign In
</button>
</form>
</div>
</div>
);
}