Google Login IN HTML CSS JS

 index.html


<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login with Google</title>
</head>

<body>
    <h1>Login with Google</h1>
    <button onclick="oauthSignIn()">Login with Google</button>

    <script>
        function oauthSignIn() {
            var oauth2Endpoint = 'https://accounts.google.com/o/oauth2/v2/auth';
            // ADD HERE YOUR CLIENT ID
            var clientId = 'YOUR_CLIENT_ID';
            var redirectUri = 'http://127.0.0.1:5500/login.html';
            var scope = 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email';


            var params = {
                'client_id': clientId,
                'redirect_uri': redirectUri,
                'response_type': 'token',
                'scope': scope,
                'include_granted_scopes': 'true',
                'state': 'pass-through value'
            };

            var queryString = Object.keys(params).map(key => key + '=' + encodeURIComponent(params[key])).join('&');

            var authUrl = oauth2Endpoint + '?' + queryString;
            window.location.href = authUrl;
        }
    </script>
</body>

</html>



login.html





<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login</title>
</head>

<body>
    <script>
        function parseHashParams() {
            var hashParams = {};
            var e, r = /([^&;=]+)=?([^&;]*)/g,
                q = window.location.hash.substring(1);
            while (e = r.exec(q)) {
                hashParams[e[1]] = decodeURIComponent(e[2]);
            }
            return hashParams;
        }

        var params = parseHashParams();
        var accessToken = params.access_token;

        if (accessToken)
            getUserProfile(accessToken);

        function getUserProfile(accessToken) {
            var peopleEndpoint = 'https://people.googleapis.com/v1/people/me';
            var url = peopleEndpoint + '?access_token=' + encodeURIComponent(accessToken) + '&personFields=emailAddresses,names,photos';

            fetch(url)
                .then(response => {
                    if (response.ok) {
                        return response.json();
                    } else {
                        throw new Error('Failed to retrieve user profile: ' + response.statusText);
                    }
                })
                .then(data => {
                    document.write("<pre>");
                    document.write(JSON.stringify(data, null, 2))
                    document.write("</pre>");
                })
                .catch(error => {
                    console.error('Error retrieving user profile:', error);
                });
        }
    </script>
</body>

</html>

Comments

Popular posts from this blog

Express Production Setup - 4 | HELMET

Express Production Setup - 3 | CORS

Ensuring File Creation in the Current Directory when Converting Python (.py) Script to Executable (.exe).