Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Login cookie expiry field #183

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions assets/build/css/settings.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion assets/build/js/login.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array(), 'version' => '57f26158d9cbe8f4');
<?php return array('dependencies' => array(), 'version' => '7f0bbc0e8908f0a8');
2 changes: 1 addition & 1 deletion assets/build/js/login.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions assets/build/js/login.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions assets/build/js/settings.asset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php return array('dependencies' => array(), 'version' => 'ce4f6367e4a9bd69');
1 change: 1 addition & 0 deletions assets/build/js/settings.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions assets/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"/build/js/login.js": "/build/js/login.js",
"/build/js/login.asset.php": "/build/js/login.asset.php",
"/build/js/settings.js": "/build/js/settings.js",
"/build/js/settings.asset.php": "/build/js/settings.asset.php",
"/build/js/block-button.js": "/build/js/block-button.js",
"/build/js/block-button.asset.php": "/build/js/block-button.asset.php",
"/build/css/settings.css": "/build/css/settings.css",
"/build/css/login.css": "/build/css/login.css",
"/build/images/google_light.png": "/build/images/google_light.png",
"/build/js/onetap.js": "/build/js/onetap.js",
Expand Down
159 changes: 159 additions & 0 deletions assets/src/js/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/**
* JS for Settings page.
*
* @package login-with-google
*/

/**
* Settings page JS.
*/
const WpGoogleSettings = {

/**
* Init method.
*
* @return void
*/
init() {
OneTapLoginSettings.init();
LoginCookieExpiry.init();
},
}

const LoginCookieExpiry = {
/**
* Init method.
*
* @return void
*/
init() {
this.disallowNonNumbers = this.disallowNonNumbers.bind( this );
this.addEventListeners = this.addEventListeners.bind( this );
this.updateCookieWarning = this.updateCookieWarning.bind( this );

this.cookieExpiryRow = document.querySelector( '.cookie-expiry-row' );
if ( ! this.cookieExpiryRow ) {
return;
}

this.cookieExpiry = this.cookieExpiryRow.querySelector( '#cookie-expiry' );

if ( ! this.cookieExpiry ) {
return;
}

this.cookieExpiryWarning = this.cookieExpiryRow.querySelector( '.warning' );

this.addEventListeners();
},

/**
* Add event listeners.
*
* @return void
*/
addEventListeners() {
this.cookieExpiry.addEventListener( 'input', this.updateCookieWarning );
this.cookieExpiry.addEventListener( 'keypress', this.disallowNonNumbers );
},

/**
* Disallow anything else than numbers.
*
* @param {object} event
*/
disallowNonNumbers( event ) {
if ( event.key.length === 1 && ! event.key.match( /[0-9]/ ) ) {
event.preventDefault();
}
},

/**
* Update cookie expiry warning.
*
* @return void
*/
updateCookieWarning() {

if ( ! this.cookieExpiryWarning ) {
return;
}

const days = parseInt( this.cookieExpiry.value );

if ( ( days > 14 ) ) {
this.cookieExpiryWarning.classList.remove( 'hidden' );
} else {
this.cookieExpiryWarning.classList.add( 'hidden' );
}
},
}

/**
* One Tap Login Settings.
*/
const OneTapLoginSettings = {

/**
* Init method.
*
* @return void
*/
init() {

this.setInitialState = this.setInitialState.bind( this );
this.addEventListeners = this.addEventListeners.bind( this );
this.toggleOneTapLoginScreenRow = this.toggleOneTapLoginScreenRow.bind( this );

this.oneTapLoginCheckBox = document.querySelector( '#one-tap-login' );

if ( ! this.oneTapLoginCheckBox ) {
return;
}

this.oneTapLoginScreenRow = document.querySelector( '.one-tap-login-screen-row' );

if ( ! this.oneTapLoginScreenRow ) {
return;
}

this.addEventListeners();
this.setInitialState();

},

/**
* Add event listeners.
*
* @return void
*/
addEventListeners() {
this.oneTapLoginCheckBox.addEventListener( 'change', this.toggleOneTapLoginScreenRow );
},

/**
* Set initial state.
*
* @return void
*/
setInitialState() {
if ( this.oneTapLoginCheckBox.checked ) {
this.oneTapLoginScreenRow.classList.remove( 'hidden' );
} else {
this.oneTapLoginScreenRow.classList.add( 'hidden' );
}
},

/**
* Toggle one tap login screen row.
*
* @return void
*/
toggleOneTapLoginScreenRow() {
this.oneTapLoginScreenRow.classList.toggle( 'hidden' );
}
}

document.addEventListener( 'DOMContentLoaded', () => {
WpGoogleSettings.init();
} );
42 changes: 42 additions & 0 deletions assets/src/scss/settings.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
.one-tap-login-screen-row.hidden {
display: none;
}

.cookie-expiry-row {

input[type=number] {
-moz-appearance: textfield;

&::-webkit-inner-spin-button,
&::-webkit-outer-spin-button {
-webkit-appearance: none;
margin: 0;
}
}

.cookie_expiry_settings_wrapper {
display: flex;
gap: 8px;
align-items: center;

input {
flex: 0 1 200px;
}
}

.human-readable-cookie-expiry {
margin: 0;

&.hidden {
display: none;
}
}

.warning {
color: #f00;

&.hidden {
display: none;
}
}
}
4 changes: 3 additions & 1 deletion assets/webpack.mix.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ mix.copy( 'src/images', 'build/images' )
.copy( 'src/js/onetap.js', 'build/js' )
.minify( 'build/js/onetap.js' )
.js( 'src/js/login.js', 'build/js' )
.js( 'src/js/settings.js', 'build/js' )
.block( 'src/js/block-button.js', 'build/js' )
.sass( 'src/scss/login.scss', 'build/css' );
.sass( 'src/scss/login.scss', 'build/css' )
.sass( 'src/scss/settings.scss', 'build/css' );
63 changes: 63 additions & 0 deletions src/Modules/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,27 @@ class Assets implements ModuleInterface {
*/
const LOGIN_BUTTON_STYLE_HANDLE = 'login-with-google';

/**
* Handle for Settings Page style.
*
* @var string
*/
const SETTINGS_PAGE_STYLE_HANDLE = 'login-with-google-settings';

/**
* Handle for Settings Page assets.
*
* @var string
*/
const SETTINGS_PAGE_SCRIPT_HANDLE = 'login-with-google-settings-script';

/**
* Settings page hook suffix.
*
* @var string
*/
const SETTINGS_PAGE_HOOK_SUFFIX = 'settings_page_login-with-google';

/**
* Module name.
*
Expand All @@ -46,6 +67,48 @@ public function name(): string {
*/
public function init(): void {
add_action( 'login_enqueue_scripts', [ $this, 'enqueue_login_styles' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_scripts' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_styles' ] );
}

/**
* Enqueue styles for admin.
*
* @param string $hook_suffix Current page hook.
*
* @return void
*/
public function enqueue_admin_styles( string $hook_suffix ): void {
if ( 'settings_page_login-with-google' !== $hook_suffix ) {
return;
}

if ( ! wp_style_is( self::SETTINGS_PAGE_STYLE_HANDLE, 'registered' ) ) {
$this->register_style( self::SETTINGS_PAGE_STYLE_HANDLE, 'build/css/settings.css' );
}

wp_enqueue_style( self::SETTINGS_PAGE_STYLE_HANDLE );
}

/**
* Enqueue scripts and styles for admin.
*
* @param string $hook_suffix Current page hook.
*
* @return void
*/
public function enqueue_admin_scripts( string $hook_suffix ): void {
if ( 'settings_page_login-with-google' !== $hook_suffix ) {
return;
}

$filename = ( defined( 'WP_SCRIPT_DEBUG' ) && true === WP_SCRIPT_DEBUG ) ? 'settings.min.js' : 'settings.js';

if ( ! wp_script_is( self::SETTINGS_PAGE_SCRIPT_HANDLE, 'registered' ) ) {
$this->register_script( self::SETTINGS_PAGE_SCRIPT_HANDLE, 'build/js/' . $filename );
}

wp_enqueue_script( self::SETTINGS_PAGE_SCRIPT_HANDLE );
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Modules/OneTapLogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ public function authenticate(): void {
}

$wp_user = $this->authenticator->authenticate( $user );

// Adding filter to modify cookie expiry for one tap login.
add_filter( 'auth_cookie_expiration', [ $this->settings, 'modify_cookie_expiry' ] );
$this->authenticator->set_auth_cookies( $wp_user );
remove_filter( 'auth_cookie_expiration', [ $this->settings, 'modify_cookie_expiry' ] );
}
}
Loading
Loading