How to register a user via json from ionic app to symfony2 - ajax

How can I register a user from an ionic app via json at my backend.
My backend is build with symfony2 and fos userbundle.
I implemented authentication/authorization via json web token which works seamless.
But I don't know how to register a user and write him through my user entity to the database.
Can anyone give my a approach on hopw to realize this or much better anyone has a little example for this issue.
thanks in advance
bambamboole
edit:
i've created a repository # github:
https://github.com/bambamboole/symfony-jwt

Here a simple example to register a user from an AJAX call using FOSRest and FOSUser bundles :
public function postUserAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$parameters = $request->request->all();
$user = new User();
$user->setUsername($parameters["username"]);
$user->setEmail($parameters["mail"]);
$user->setPlainPassword($parameters["password"]);
$user->setEnabled(false);
$user->setFirstname($parameters["firstname"]);
$user->setLastname($parameters["lastname"]);
$tokenGenerator = $this->get('fos_user.util.token_generator');
$token = $tokenGenerator->generateToken();
$user->setConfirmationToken($token);
$em->persist($user);
$em->flush();
$this->get('fos_user.mailer')->sendConfirmationEmailMessage($user);
$view = View::create()
->setStatusCode(200)
->setData($user);
return $this->get('fos_rest.view_handler')->handle($view);
}
Hope it will help you.

Related

Laravel API request to a cloud model AI

I've been working on a in a route of validation for the user in Laravel API to connect to a NLP model in Google Cloud. Making the request go to the API them the API makes a request to a model in Google Cloud. After the prediction is given I pass back to the user.
The problem seems that it takes a certain almost of time to receive a answer from the model so I end up answering a empty JSON to the user.
I wish to ask what is better way to proceed in this situation.
public function request_To_The_Model(Request $request)
{
$data = $request->json()->all();
$response = Http::post(URL, [
$data['sentence']
]);
$body = $response->json();
return response()->json($body);
}

Acessing auth user attribute

I am in the guzzle controller making a request to an external api.
I wanna use an id from the user who is logged in.
I have been doing the request with a static id, but now i want it dynamically.
I tried like this:
$science = Auth::user()->science_id;
$client = new Client(['headers' => ['Accept' => 'application/json']]);
$request = $client->get(
'https://url_to_the_api/'.$science.'/degree',
[
'auth' => ['client', 'secret'],
]
);
$data = $request->getBody()->getContents();
return $data;
And i have the error
500(internal server error)
and this message:
"Trying to get property 'science_id' of non-object"
What am i missing?
Thanks for your time
If you are using it in web app then make sure you first check if user is already authenticated by using auth middleware or manually by using Auth::check() function.
Or
If you are trying to hit this by api that will not work here because session will not be maintained in that case. That's why JWT tokens were introduced to maintain the state of an application.
I've solved it like this:
$science = auth('api')->user()->science_id;
Thanks for the help!

To create restful api for android team in Laravel 5.2

I am currently creating Rest API for Android Team using Laravel 5.2 and testing the API in RESTCLIENT.
I am just trying to get the form values which are entered in Restclient with POST method.
I have problem with POST method.. I m just not able to get the form field values. My API for GET method works fine.
Kindly guide me.
Thanks
This is how you handle POST request in laravel. Be it 5.2 or 5.4.
Route::post('books', 'BookController#store');
Controller
pubic function store(Request $request)
{
$name = $request->name;
$name = $request->get('name');
$name = request('name'); //global helper
//Your testing with api so you should return to api if you want to see it
return response()->json($name);
}
Then check in Android console if you receive $name in JSON format.

What to do when lucadegasperi oauth2 server for laravel gets caught by auth middleware?

So currently building an oauth2 server with:
https://github.com/lucadegasperi/oauth2-server-laravel/blob/master/docs/authorization-server/auth-code.md
Auth Grant
laravel 5.2
Now no where in the instructions does it address what to do when the user is not logged in. (which most times will be the case)
So in that scenario - the user hits the auth middleware kicking them to the login screen... but what to do after that? There is nothing passed to the login page? so how do i know where to redirect the user back to?
Now yes of course I can just do this on my own, but before I do that I just want to make sure I am not missing anything? again it was not address in the documentation, so I can only assume this was thought through?
Let me know your thoughts.
Steve
So just ended up doing a work around in my Authenticate.php file. Incase anyone else is curious I did this:
$params = [];
if($request->has('client_id'))
$params['client_id'] = $request->client_id;
if($request->has('redirect_uri'))
$params['redirect_uri'] = $request->redirect_uri;
if($request->has('response_type'))
$params['response_type'] = $request->response_type;
if($request->has('scope'))
$params['scope'] = $request->scope;
if($request->has('state'))
$params['state'] = $request->state;
return redirect()->route('login', $params);
//return redirect()->guest('login');
Passed this to my loginController. Then in loginController:
$params = [];
if($this->request->has('redirect_uri'))
$params['redirect_uri'] = $this->request->redirect_uri;
if($this->request->has('response_type'))
$params['response_type'] = $this->request->response_type;
if($this->request->has('scope'))
$params['scope'] = $this->request->scope;
if($this->request->has('state'))
$params['state'] = $this->request->state;
if($this->request->has('client_id'))
{
$params['client_id'] = $this->request->client_id;
//dd($params);
return redirect()->route('oauth.authorize.get', $params);
}
Let me know if you see any issues.
Cheers
Citti

Symfony keep user logged after manual authentication

To authenticate manually a user I used the following code:
$user = new User($token, null, array("ROLE_USER"));
$token = new UsernamePasswordToken($user, null, 'secured_area', array("ROLE_USER"));
$this->get("security.context")->setToken($token);
$event = new InteractiveLoginEvent($request, $token);
$this->get("event_dispatcher")->dispatch("security.interactive_login", $event);
The problem is that when I change page (also same controller) I lost the token/session.
Sometimes I get this: There is no user provider for user "Acme\MyBundle\Security\User".
How I should configure the secured area with pattern: ^/?
secured_area:
pattern: ^/
Thanks
In FosUserBundle you can see an example of how to do Manual login correctly.

Resources