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
From what I can see, the code for authentication could completely be extracted into an own package and being reused for cookies-based auth in routes, right?
How you going to use it? Give a usage example(s)
Example code could look like this:
Client
import{Accounts}from'meteor/accounts-base'import{DDP}from'meteor/ddp-client'import{Meteor}from'meteor/meteor'// cookie injected from outside to let host control cookie optionsexportconstcookieAuth=cookie=>{constsetTokenCookie=()=>{if(Meteor.connection._lastSessionId){cookie.set('x_mtok',Meteor.connection._lastSessionId,{path: '/',sameSite: 'Lax'});if(Meteor.isCordova&&this.allowQueryStringCookies){cookie.send();}}};const_accounts=(Package&&Package['accounts-base']&&Package['accounts-base'].Accounts) ? Package['accounts-base'].Accounts : undefined;if(_accounts){DDP.onReconnect((conn)=>{conn.onReconnect=setTokenCookie;});Meteor.startup(setTokenCookie);_accounts.onLogin(setTokenCookie);}}
Server
import{Meteor}from'meteor/meteor'exportconstgetUser=(req,res,next)=>{if(req.userId&&req.user){returnnext()}letmtok=nullif(req.headers['x-mtok']){mtok=req.headers['x-mtok']}else{constcookie=req.Cookiesif(cookie.has('x_mtok')){mtok=cookie.get('x_mtok')}}if(mtok){constuserId=getUserIdFromToken(mtok)if(userId){req.userId=()=>userIdreq.user=(()=>{letuserreturn()=>{if(!user){user=Meteor.users.findOne(userId)}returnuser}})()}}returnnext()}constisObject=obj=>typeofobj==='object'constgetUserIdFromToken=(xmtok)=>{if(!xmtok)returnnullconstsessions=Meteor.server.sessionsconstsessionIsMap=sessionsinstanceofMapconstsessionIsObject=isObject(sessions)// throw an error upon an unexpected type of Meteor.server.sessions in order to identify breaking changesif(!sessionIsMap||!sessionIsObject){thrownewError('Received incompatible type of Meteor.server.sessions')}if(sessionIsMap&&sessions.has(xmtok)&&isObject(sessions.get(xmtok))){// to be used with >= Meteor 1.8.1 where Meteor.server.sessions is a Mapreturnsessions.get(xmtok).userId}elseif(sessionIsObject&&xmtokinsessions&&isObject(sessions[xmtok])){// to be used with < Meteor 1.8.1 where Meteor.server.sessions is an Objectreturnsessions[xmtok].userId}returnnull}
What do you think about it? Also, do you have any concerns regarding security on that approach?
The text was updated successfully, but these errors were encountered:
@jankapunkt I'd turn it into middleware level API option, and call it http-auth (open to suggestions on name). Would be useful if one want to use Meteor's accounts while building logic on HTTP/REST level
I think we make http-auth an extra middleware package but make it remain as dependency of this package so the original functionality is untouched. Other suggestions @s-ol@menelike (mentioned you, since you both were involved in some of the former cookie issues, right)?
I have a suggestion:
I currently implement connect route authentication with
ostrio:cookies
and basically the code from this package:From what I can see, the code for authentication could completely be extracted into an own package and being reused for cookies-based auth in routes, right?
Example code could look like this:
Client
Server
What do you think about it? Also, do you have any concerns regarding security on that approach?
The text was updated successfully, but these errors were encountered: