3
votes

I have used PHP SDK-4 for Facebook login in CakePHP 3 (beta version) which works fine.
Now, I'm in need to fetch user data based on FB login and authenticate users. Am trying with Cake's Auth component. Initially, while trying to Auth users,

$this->Auth->setUser($user)

Got

Error: Session was already started
as we require session_start() for Facebook login.
1- Tried with enter link description here, and sessions [session_write_close()] etc..still it did not work.
Could I get some shot on best way to authenticate users with Facebook login in site?
1
Cakes Auth system already started a session, thats what the error as well tells you pretty clear. You need to figure out how exactly FB auth works and write an auth adapter for Cake3. - floriank
@burzum I think it's the other way around, the manual session_start() started the session, if it was CakePHP that started the session, then Session::start() wouldn't reach the point where this error message is thrown. - ndm
@ndm well, this depends on where he called session_start() but now that I'm reading it again I think you're right. - floriank

1 Answers

3
votes

CakePHPs sessions are lazy started, that is, they are being started once your try to access the session in some way, and in case the session was started manually in beforehand, you'll receive that error, see Session::start().

You can easily workaround this by manually starting the session via CakePHP. The session object is available in the current request, so for example in your controller before using the SDK you could simply do something like

$this->request->session()->start();

and then the Facebook SDK should be able to pick it up.

As burzum already mentioned in the comments, the authentication should better be wrapped up in an authentication handler.

I would suggest having a look at HybridAuth, there's also a CakePHP plugin for seamless integration into CakePHPs auth mechanism, this might give you some ideas for a custom implemenation in case you need to use the v4 SDK, which isn't yet supported by HybridAuth.