Calling Javascript function from Laravel controlle

2019-09-13 10:59发布

问题:

I want to call a firebase auth function (which is in Javascript) from my controller and I want the returned token back in my controller, but I do not know how to proceed.

I know how to post ajax request to controller and get data back, however I don't know how to make a request the other way around.

I would be thankful if you can put me and other future users in right direction.

回答1:

This is not going to work.

You know a webserver is always supposed to be online. That way, you can always make a request.

A client-side (user), does not need, and isn't always online. If they close the browser window, you can't interact with them anymore. The server would have a hard time checking what user just went down, out of the thousands of users you have.

Making a call to a webserver is possible, but making a call to a client is not.

There are, next to that reason, security issues if you would be able to make a request to the client-side.

What you should probably do, is make a seperate request for the authentication token from Javascript, make Laravel call the Firebase AUTH server and then return the token it received.


update

It is possible to make requests to a client using HTTP2, by using server-push. To do that properly, especially with an authentication token, you will need some experience in building webservices and security.



回答2:

PHP is a server side language and Javascript is client side. You cannot javascript on server and vice-versa. If you just need to generate JWT token, there are PHP libraries available for that already. But if you really want to execute javascript on server, that I think is not the case, you'll have to use nodejs