File size: 1,276 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import config from '@automattic/calypso-config';
import { useEffect, useRef } from 'react';

function getFormAction( redirectTo ) {
	const subdomainRegExp = /^https?:\/\/([a-z0-9-]+)\.wordpress\.com(?:$|\/)/;
	const hostname = config( 'hostname' );
	let subdomain = '';

	if (
		subdomainRegExp.test( redirectTo ) &&
		hostname !== 'wpcalypso.wordpress.com' &&
		hostname !== 'horizon.wordpress.com'
	) {
		const subdomainMatch = redirectTo.match( subdomainRegExp );
		if ( subdomainMatch && subdomainMatch[ 1 ] !== 'public-api' ) {
			subdomain = subdomainMatch[ 1 ] + '.';
		}
	}

	return `https://${ subdomain }wordpress.com/wp-login.php`;
}

export default function WpcomLoginForm( {
	redirectTo,
	authorization,
	pwd = '',
	log,
	rememberMe = false,
} ) {
	const form = useRef();

	useEffect( () => {
		form.current.submit();
	}, [] );

	return (
		<form method="post" action={ getFormAction( redirectTo ) } ref={ form }>
			<input type="hidden" name="log" value={ log } />
			<input type="hidden" name="pwd" value={ pwd } />
			<input type="hidden" name="authorization" value={ authorization } />
			<input type="hidden" name="redirect_to" value={ redirectTo } />
			{ rememberMe ? <input type="hidden" name="rememberme" value="forever" /> : undefined }
		</form>
	);
}