You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I added the ability to specify multiple accounts to the fake back end. and added a role element
This was mainly so i could experiment with dynamic navigation rendering. It may be useful for others.
Its my first real javascript hack, mainly php and java/kotlin up to now. so be gentle. :-)
The code takes care of generating and verifying multiple unique fake jwt tokens.
The core of the get users code could do with moving to a separate function so that it can be used to authenticate more fake be urls.
===== fake-backend.js =======
exportfunctionconfigureFakeBackend(){letusers=[{id: 1,username: 'test',password: 'test',firstName: 'Test',lastName: 'User','role': ["user"]},{id: 2,username: 'admin',password: 'admin',firstName: 'Admin',lastName: 'User','role': ["admin","user"]}];letrealFetch=window.fetch;window.fetch=function(url,opts){returnnewPromise((resolve,reject)=>{// wrap in timeout to simulate server api callsetTimeout(()=>{// authenticateif(url.endsWith('/users/authenticate')&&opts.method==='POST'){// get parameters from post requestletparams=JSON.parse(opts.body);// find if any user matches login credentialsletfilteredUsers=users.filter(user=>{returnuser.username===params.username&&user.password===params.password;});if(filteredUsers.length){// if login details are valid return user details and fake jwt tokenletuser=filteredUsers[0];letresponseJson={id: user.id,username: user.username,firstName: user.firstName,lastName: user.lastName,role: user.role,token: 'fake-jwt-token-'+user.id};resolve({ok: true,text: ()=>Promise.resolve(JSON.stringify(responseJson))});}else{// else return errorreject('Username or password is incorrect');}return;}// get usersif(url.endsWith('/users')&&opts.method==='GET'){// check for fake auth token in header and return users if valid, this security is implemented server side in a real applicationif(opts.headers){letuserid=Number(opts.headers.Authorization.replace(/^\D+/g,''));// replace all leading non-digits with nothingletfound=false;// Horribly inefficient linear search but allows the ids to be non sequential and sparse. for(leti=0;i<users.length;i++){if(users[i].id===userid){found=true;break;}}if(found&&opts.headers.Authorization===('Bearer fake-jwt-token-'+userid)){resolve({ok: true,text: ()=>Promise.resolve(JSON.stringify(users))});return;}}// return 401 not authorised if token is null or invalidreject('Unauthorised');return;}// pass through any requests not handled aboverealFetch(url,opts).then(response=>resolve(response));},500);});}}
The text was updated successfully, but these errors were encountered:
I added the ability to specify multiple accounts to the fake back end. and added a role element
This was mainly so i could experiment with dynamic navigation rendering. It may be useful for others.
Its my first real javascript hack, mainly php and java/kotlin up to now. so be gentle. :-)
The code takes care of generating and verifying multiple unique fake jwt tokens.
The core of the get users code could do with moving to a separate function so that it can be used to authenticate more fake be urls.
===== fake-backend.js =======
The text was updated successfully, but these errors were encountered: