Coder Social home page Coder Social logo

atomjoy / google-login-button Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 118 KB

Funkcja Zaloguj się przez Google umożliwia łatwe i bezpieczne logowanie się w aplikacjach lub usługach innych firm przy użyciu konta Google. Przycisk logowania google oauth na stronę internetową www (oAuth2, Vue3, Laravel).

Home Page: https://github.com/atomjoy/google-login-button

Vue 28.92% PHP 52.89% HTML 18.20%
google-login-button google-oauth-login google-oauth2-jwt google-przycisk-logowania vue3-google-signin google-login-button-vue3-laravel vue3-laravel-google-login-button vue3-google-login

google-login-button's Introduction

Logowanie za pomocą konta Google (Google signin button)

Funkcja Zaloguj się przez Google umożliwia łatwe i bezpieczne logowanie się w aplikacjach lub usługach innych firm przy użyciu konta Google. Przycisk logowania Google oAuth na stronę internetową www (oAuth2).

Utwórz swoją aplikację

https://console.cloud.google.com

Dodaj klucz oAuth do swojej aplikacji (pobierz plik json klucza z clientId)

Dodaj adresy uri dla klucza wideo: https://www.youtube.com/watch?v=EaSWnk5fLdc

Generator przycisku logowania Google

https://developers.google.com/identity/gsi/web/tools/configurator?hl=en

Css style

Likwidacja białego tła iframe dla dark mode w przeglądarce.

iframe {
	color-scheme: light !important;
}

Przycisk z Javascript Api

Zmień google klient id: <GOOGLE_CLIENT_ID>.apps.googleusercont.com

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

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1"></meta>
<body>
  <script src="https://accounts.google.com/gsi/client" async defer></script>
  <script>		
    function onSignout() {
      google.accounts.id.disableAutoSelect();
    }
    function onRevoke(uid = '1618033988749895') {
      google.accounts.id.revoke(uid, done => {
        console.log(done.error);
      });
    }
    function handleCredentialResponse(response) {
      console.log("Encoded JWT ID token: " + response.credential);
      // Send to backend server and uthenticate user
      // Confirm jwt token (backend) and get google userinfo from: https://oauth2.googleapis.com/tokeninfo?id_token={id_token}
    }

    window.onload = function () {
      window.google.accounts.id.initialize({
        client_id: "<GOOGLE_CLIENT_ID>.apps.googleusercontent.com",
        callback: handleCredentialResponse,
        itp_support: true
      });
      google.accounts.id.renderButton(
        document.getElementById("buttonDiv"),
        { theme: "outline", size: "large" }
      );
      // google.accounts.id.prompt(); // also display the One Tap dialog
      google.accounts.id.prompt((notification) => {
        if (notification.isNotDisplayed()) {
          console.log(notification.getNotDisplayedReason())
        } else if (notification.isSkippedMoment()) {
          console.log(notification.getSkippedReason())
        } else if(notification.isDismissedMoment()) {
          console.log(notification.getDismissedReason())
        }

        if (notification.isNotDisplayed() || notification.isSkippedMoment()) {
          // try next provider if OneTap is not displayed or skipped
        }
      });
    }
  </script>
  <div id="buttonDiv"></div>
</body>
</html>

Przycisk z Html Api

Zmień google klient id: <GOOGLE_CLIENT_ID>.apps.googleusercont.com

<!DOCTYPE html>
<html lang="pl">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1"></meta>
<body>
  <script src="https://accounts.google.com/gsi/client" async defer></script>
  <script>		
    function onSignout() {
      google.accounts.id.disableAutoSelect();
    }
    function handleCredentialResponse(response) {
      console.log("Encoded JWT ID token: " + response.credential);
      // Send to backend server and uthenticate user
      // Confirm jwt token (backend) and get google userinfo from: https://oauth2.googleapis.com/tokeninfo?id_token={id_token}
    }
  </script>

  <div id="g_id_onload" data-client_id="<GOOGLE_CLIENT_ID>.apps.googleusercontent.com" data-context="signin" data-ux_mode="popup" data-callback="handleCredentialResponse" data-nonce="" data-itp_support="true"></div>
  <div class="g_id_signin" data-type="standard" data-shape="rectangular" data-theme="filled_blue" data-text="signin_with," data-size="large" data-logo_alignment="left"></div>
</body>
</html>

JWT token walidacja

Walidacja tokena jwt w js lub na serverze backendowym i pobranie danych userinfo.

// Validate jwt token on your backend server (this is sample with google server)
// Login user on backend server with session
// Return logged user detail from backend here
async function verifyTokenUserInfo(id_token) {
	try {
		// Javascript (tests only)
		// JWT token validation with google server
		const res = await axios.get(`https://oauth2.googleapis.com/tokeninfo?id_token=${id_token}`)
		console.log('Logged user detail', res)
		return res.data
    
    		// Server url
    		const callback_url = '/oauth/google'

		// Laravel backend server JWT token validation with curl request to google url: https://oauth2.googleapis.com/tokeninfo?id_token=
    		// Login user on backend server and return userinfo details
		const resb = await axios.get(`${callback_url}?id_token=${id_token}`)
		console.log('Logged user detail', resb)
		return resb.data.userinfo
	} catch (err) {
		console.log('Login error', err)
		user.value = null
		return null
	}
}

Uruchom server Laravel

Dodaj w google+ api do klucza uri: http://localhost i http://localhost:8000 (Przy błędzie invalid origin ... puste okienko logowania)

php artisan serve --host=localhost --port=8000

Backend

Images

google-login-button's People

Contributors

atomjoy avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.