2023-08-14 23:33:21 +00:00
const login _messages = document . getElementById ( 'login-messages-inpt' ) ;
const otp _messages = document . getElementById ( 'otp-messages-inpt' ) ;
const forgot _messages = document . getElementById ( 'forgot-messages' ) ;
const target _url = ( ) => {
const url _params = new URL ( window . location . toLocaleString ( ) ) . searchParams ;
const target _url = url _params . get ( 'o' ) ;
return target _url ? target _url : '/' ;
} ;
const sign _up = ( ) => {
if ( SIGNIN _URL ) {
location . href = SIGNUP _URL ;
}
} ;
const on _forgot _passwd = ( ) => {
if ( RESET _URL ) {
if ( forgot _messages ) {
forgot _messages . classList . remove ( 'hidden' ) ;
forgot _messages . innerHTML = '<p>For <u>password reset</u> you need:</p><p> a <b>valid user, email</b> and a <b>verified TOTP</b> code.</p><p>Otherwise contact with Service Administrators.</p>'
}
const main _form = document . getElementById ( 'main-form' ) ;
const otp _form = document . getElementById ( 'otp-form' ) ;
const btn _otp _forgot _passwd = document . getElementById ( 'otp-forgot-password' ) ;
const otp _login _link = document . getElementById ( 'otp-login-link' )
const btn _otp _signin = document . getElementById ( 'otp-signin-button' ) ;
const name = document . getElementById ( 'login-name-inpt' ) . value ;
if ( name == '' ) {
login _messages . innerHTML = 'Enter a name' ;
return ;
}
main _form . classList . add ( 'hidden' ) ;
if ( btn _otp _forgot _passwd ) { btn _otp _forgot _passwd . classList . remove ( 'hidden' ) } ;
if ( otp _login _link ) { otp _login _link . classList . remove ( 'hidden' ) } ;
if ( btn _otp _signin ) { btn _otp _signin . classList . add ( 'hidden' ) } ;
otp _form . classList . remove ( 'hidden' ) ;
}
} ;
const forgot _passwd = async ( ) => {
const otp _loading = document . getElementById ( 'otp-loading' ) ;
const otp _btns = document . getElementById ( 'otp-btns' ) ;
const elem _otp _auth = document . getElementById ( 'login-totp-inpt' ) ;
const elem _name = document . getElementById ( 'login-name-inpt' ) ;
const otp _form = document . getElementById ( 'otp-form' ) ;
let name = '' ;
if ( elem _name ) { name = elem _name . value ; }
let otp _auth = '' ;
if ( elem _otp _auth ) {
otp _auth = elem _otp _auth . value ;
if ( otp _auth == '' ) {
otp _messages . innerHTML = 'Please enter a valid TOTP code' ;
return ;
}
const numbers = /^[0-9]+$/ ;
if ( ! otp _auth . match ( numbers ) || otp _auth . length != totp _digits ) {
otp _messages . innerHTML = 'Please enter a valid TOTP code' ;
return ;
}
}
if ( RESET _URL ) {
const input _data = { name , value : otp _auth } ;
const response = await fetch ( RESET _URL , {
method : 'POST' ,
body : JSON . stringify ( input _data ) ,
headers : {
'Content-type' : 'application/json; charset=UTF-8' ,
// 'cookie': document.cookie,
}
} ) ;
if ( otp _loading ) { utils . css ( 'show' , otp _loading ) ; }
if ( otp _btns ) { utils . css ( 'hide' , otp _btns ) ; }
if ( response . ok && response . status === 200 ) {
if ( otp _loading ) { utils . css ( 'hide' , otp _loading ) ; }
const data = await response . text ( ) ;
if ( otp _form ) { utils . css ( 'hide' , otp _form ) ; }
if ( forgot _messages ) { forgot _messages . innerHTML = data }
setTimeout ( ( ) => { location . href = target _url ( ) } , settings . toast _copy _timeout ) ;
} else {
if ( otp _loading ) { utils . css ( 'hide' , otp _loading ) ; }
if ( otp _btns ) { utils . css ( 'show' , otp _btns ) ; }
if ( forgot _messages ) { forgot _messages . innerHTML = 'Errors in password reset' ; }
return false ;
}
}
} ;
const set _name = ( elem ) => {
if ( login _messages . innerHTML != '' ) { login _messages . innerHTML = '' ; }
if ( forgot _messages ) { forgot _messages . classList . add ( 'hidden' ) ; }
} ;
const set _password = ( elem ) => {
if ( login _messages . innerHTML != '' ) { login _messages . innerHTML = '' ; }
if ( forgot _messages ) { forgot _messages . classList . add ( 'hidden' ) ; }
} ;
const log _in = async ( ) => {
const main _form = document . getElementById ( 'main-form' ) ;
const main _btns = document . getElementById ( 'main-btns' ) ;
const main _loading = document . getElementById ( 'main-loading' ) ;
const otp _form = document . getElementById ( 'otp-form' ) ;
const name = document . getElementById ( 'login-name-inpt' ) . value ;
if ( name == '' ) {
login _messages . innerHTML = 'Enter a name' ;
return ;
}
const password = document . getElementById ( 'login-password-inpt' ) . value ;
if ( password == '' ) {
login _messages . innerHTML = 'Please choose a password' ;
return ;
}
const data = {
name ,
password ,
//timestamp: new Date().toLocaleString(),
} ;
if ( main _btns ) { utils . css ( 'hide' , main _btns ) ; }
if ( main _loading ) { utils . css ( 'show' , main _loading ) ; }
const response = await fetch ( SIGNIN _URL , {
method : 'POST' ,
body : JSON . stringify ( data ) ,
headers : {
'Content-type' : 'application/json; charset=UTF-8' ,
// 'cookie': document.cookie,
}
} ) ;
if ( main _loading ) { utils . css ( 'hide' , main _loading ) ; }
if ( response . ok && response . status === 200 ) {
const data = await response . text ( ) ;
if ( data . includes ( 'true' ) && otp _form ) {
main _form . classList . add ( 'hidden' ) ;
otp _form . classList . remove ( 'hidden' ) ;
} else {
location . href = target _url ( ) ;
}
} else {
login _messages . innerHTML = 'Signin was not OK' ;
if ( main _btns ) { utils . css ( 'show' , main _btns ) ; }
return false ;
}
} ;
const set _totp = ( elem ) => {
if ( otp _messages . innerHTML != '' ) { otp _messages . innerHTML = '' ; }
} ;
const otp _in = async ( ) => {
const otp _btns = document . getElementById ( 'otp-btns' ) ;
const otp _loading = document . getElementById ( 'otp-loading' ) ;
const name = document . getElementById ( 'login-name-inpt' ) . value ;
if ( name == '' ) {
otp _messages . innerHTML = 'Enter a name' ;
return ;
}
const password = document . getElementById ( 'login-password-inpt' ) . value ;
if ( password == '' ) {
otp _messages . innerHTML = 'Please choose a password' ;
return ;
}
const otp _auth = document . getElementById ( 'login-totp-inpt' ) . value ;
if ( otp _auth == '' ) {
otp _messages . innerHTML = 'Please enter a valid TOTP code' ;
return ;
}
const numbers = /^[0-9]+$/ ;
if ( ! otp _auth . match ( numbers ) || otp _auth . length != totp _digits ) {
otp _messages . innerHTML = 'Please enter a valid TOTP code' ;
return ;
}
const data = {
name ,
password ,
otp _auth ,
} ;
if ( otp _btns ) { utils . css ( 'hide' , otp _btns ) ; }
if ( otp _loading ) { utils . css ( 'show' , otp _loading ) ; }
const response = await fetch ( SIGNIN _URL , {
method : 'POST' ,
body : JSON . stringify ( data ) ,
headers : {
'Content-type' : 'application/json; charset=UTF-8' ,
// 'cookie': document.cookie,
}
} ) ;
if ( otp _loading ) { utils . css ( 'hide' , otp _loading ) ; }
if ( response . ok && response . status === 200 ) {
location . href = target _url ( ) ;
} else {
if ( otp _btns ) { utils . css ( 'show' , otp _btns ) ; }
otp _messages . innerHTML = 'Signin was not OK' ;
return false ;
}
} ;
2023-09-19 01:20:29 +00:00
const on _openid = ( item ) => {
location . href = ` /openid/ ${ item } ` ;
} ;
2023-08-14 23:33:21 +00:00
const restart = ( ) => {
const main _form = document . getElementById ( 'main-form' ) ;
const main _btns = document . getElementById ( 'main-btns' ) ;
const otp _form = document . getElementById ( 'otp-form' ) ;
const otp _btns = document . getElementById ( 'otp-btns' ) ;
if ( otp _btns ) { utils . css ( 'show' , otp _btns ) ; }
if ( main _btns ) { utils . css ( 'show' , main _btns ) ; }
otp _form . classList . add ( 'hidden' ) ;
forgot _messages . classList . add ( 'hidden' ) ;
forgot _messages . innerHTML = '' ;
main _form . classList . remove ( 'hidden' ) ;
login _messages . innerHTML = '' ;
otp _messages . innerHTML = '' ;
} ;
window . addEventListener ( 'load' , ( ) => {
const show _password = document . getElementById ( 'show-password' ) ;
const input _password = document . getElementById ( 'login-password-inpt' )
const otp _logo _link = document . getElementById ( 'otp-logo-link' )
if ( show _password ) {
show _password . addEventListener ( 'click' , ( ev ) => {
ev . preventDefault ( ) ;
[ ... show _password . children ] . forEach ( it => {
it . classList . toggle ( 'hidden' ) ;
} ) ;
if ( input _password . type == 'password' ) {
input _password . type = 'text' ;
} else {
input _password . type = 'password' ;
}
} ) ;
}
if ( otp _logo _link ) { otp _logo _link . href = location . href ; }
} ) ;