mocksaml/pages/saml/login.tsx

121 lines
3.3 KiB
TypeScript
Raw Normal View History

2022-02-22 08:18:07 +00:00
import Head from 'next/head';
import { useRouter } from 'next/router';
import type { FormEvent } from 'react';
import { useEffect, useRef, useState } from 'react';
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;
const [state, setState] = useState({
username: 'jackson',
domain: 'example.com',
});
2022-02-24 06:20:00 +00:00
// 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 | HTMLSelectElement>): void => {
const { name, value } = e.currentTarget;
setState({
...state,
[name]: value,
});
2022-02-22 08:18:07 +00:00
};
const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
const { username, domain } = state;
2022-02-22 08:18:07 +00:00
const response = await fetch(`/api/saml/auth`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
email: `${username}@${domain}`,
id,
audience,
acsUrl,
providerName,
relayState,
}),
2022-02-22 08:18:07 +00:00
});
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');
2022-02-22 08:33:22 +00:00
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>
<div className='relative top-20 mx-auto w-[465px] max-w-[90%] rounded-md border p-10 text-[#145698]'>
<h2 className='mb-3 text-center text-3xl font-bold'>Login</h2>
2022-02-22 08:18:07 +00:00
<form onSubmit={handleSubmit}>
<div className='flex items-end gap-x-1'>
<div>
<label htmlFor='username' className='mb-2 block'>
Email
</label>
<input
name='username'
id='username'
ref={emailInp}
autoComplete='off'
type='text'
placeholder='jackson'
value={state.username}
onChange={handleChange}
className='input'
title='please provide a mock example.com email address'
/>
</div>
<select
name='domain'
id='domain'
className='select w-full'
2022-02-22 08:18:07 +00:00
onChange={handleChange}
value={state.domain}>
<option value='example.com'>@example.com</option>
<option value='example.org'>@example.org</option>
</select>
2022-02-18 18:12:45 +00:00
</div>
2022-02-22 08:18:07 +00:00
<div className='mt-5'>
<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'
className='input w-full'
2022-02-18 18:12:45 +00:00
/>
</div>
<button type='submit' className='button mt-8 w-full'>
2022-02-18 18:12:45 +00:00
Sign In
</button>
</form>
</div>
</div>
);
}