I have the next resource:
angular.module('app').factory 'User', [
'$resource'
($resource) ->
$resource '/users/:id/:action', {
id: '#id'
action: '#action'
},
query:
method: 'GET'
isArray: false
'update':
method: 'PUT'
transformRequest: (data) ->
JSON.stringify user: data
'resetPassword':
params: action: 'reset_password'
method: 'PUT'
]
And I want to unify it with the next one:
angular.module('app').factory "NewUser", ["RailsResource", "railsSerializer", (RailsResource, railsSerializer) ->
class NewUser extends RailsResource
#configure
url: '/users'
rootWrapping: false
serializer: railsSerializer ->
#nestedAttribute "addresses", "address"
NewUser
]
The goal is to have both of the above resources merged into a single one.
What's the best approach for doing this?
Related
assumption
Logging in from the front end and sending email and password returns user information, but trying to send a logged in user from Rails using devise token auth returns null data.
What we want to achieve
・I want to return the currently logged in user from Rails.
Code
Rails
ApplicationController
class ApplicationController < ActionController::API
include DeviseTokenAuth::Concerns::SetUserByToken
end
SessionsControlle
class Api::V1::Auth::SessionsController < ApplicationController
def index
render json: current_api_v1_user, status: :ok
end
end
Response
config: {url: '/api/v1/auth/sessions', method: 'get', headers: {…}, baseURL: 'http://localhost:3000', transformRequest: Array(1), …}
data: null // Response from Rails is null.
headers: {cache-control: 'max-age=0, private, must-revalidate', content-type: 'application/json; charset=utf-8'}
request: XMLHttpRequest {readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, onreadystatechange: ƒ, …}
status: 200
statusText: "OK"
Access from Js
auth: {
・
・
・
strategies: {
local: {
endpoints: {
・
・
・
user: { url: '/api/v1/auth/sessions', method: 'get', propertyName: false }
}
}
}
},
I'm trying to use nuxt-auth module, my settings for this module is
auth: {
cookie: false,
plugins: ['~/plugins/api.js'],
redirect: {
logout: '/login',
login: '/',
home: false
},
strategies: {
local: {
scheme: 'refresh',
token: {
property: 'token',
maxAge: 3600
},
refreshToken: {
property: 'refresh_token',
data: 'refresh_token',
maxAge: 60 * 60 * 24 * 30
},
user: {
property: 'userDetail'
},
endpoints: {
login: { url: 'http://localhost:8085/api/login_check', method: 'post', propertyName: 'token' },
refresh: { url: 'http://localhost:8085/api/token/refresh', method: 'post', propertyName: 'refresh_token' },
logout: false,
user: { url: 'http://localhost:8085/api/user/fetchactive', method: 'get' }
},
tokenRequired: true
}
}
}
My "fetchactive" API returns a JSON containing a property "userDetail" which is a string containing the email address, (I also tried to make userDetail an object but with no luck).
e.g.
{"userDetail":{"email":"my#email.test"}}
Nuxt auth keeps telling me that "User Data response does not contain field userDetail".
I also tried to set "property" to false, but Nuxt auth in that cases looks for a field named "false"...
I just can't get it to work.
Anyone can help?
using this configuration in the nuxt.config.js file worked for me
auth: {
strategies: {
local: {
user: {
property: ''
},
//other configs
}
}
//other configs
}
It's happened due to the fact that auth.user endpoint search for 'data' object by default in the response but I does not exist there, as You have Your own response object key, not contained in 'data:{...}
try to add propertyName as in example in auth.user
endpoints: {
user: { url: 'http://localhost:8085/api/user/fetchactive', method: 'get', propertyName: '' }
}
}
I have the following resource definition:
myservices.factory('User', ['$resource',
function($resource){
return $resource('/user/:screenName', {}, {
whoami: {
url: '/user',
method: 'GET',
isArray: false,
params: {
op: 'whoami'
}
},
query: {
url: '/user',
method: 'GET',
isArray: true
},
get: {
method: 'GET',
isArray: false
},
update: {
method:'GET',
params: {
op: 'update'
},
isArray: false,
},
delete: {
method:'GET',
params: {
op: 'delete'
},
isArray: false,
}
});
}]);
and I was thinking it will pass screenName as the part of URL. Unfortunately, this does not happen.
The controller code is follows:
var user = new User();
user.firstname = $scope.selectedUser.firstName;
user.lastname = $scope.selectedUser.lastName;
user.screenName = $scope.selectedUser.screenName;
user.password1 = $scope.selectedUser.password1;
user.password2 = $scope.selectedUser.password2;
user.roles = $scope.selectedUser.roles;
user.maximalCalories = $scope.selectedUser.maximalCalories;
user.$update();
Actually it passes:
GET http://localhost:8080/user?op=update HTTP/1.1
Host: localhost:8080
...
Accept: application/json, text/plain, */*
...
Referer: http://localhost:8080/app/index.html
i.e. it passes neither parameters except explicit one.
UDPATE
If I do
User.$update(
{
firstname : $scope.selectedUser.firstName,
lastname : $scope.selectedUser.lastName,
screenName : $scope.selectedUser.screenName,
password1 : $scope.selectedUser.password1,
password2 : $scope.selectedUser.password2,
roles : $scope.selectedUser.roles,
maximalCalories : $scope.selectedUser.maximalCalories,
}
);
I get an exception TypeError: User.$update is not a function
UPDATE 2
Apparently, Angular adds function $update to the object and function update to the class, and if I have object at LHS, it will pass only by POST...
You are not passing screenName to resource. Should be:
user.$update({screenName: $scope.selectedUser.screenName});
The problem is you have wrong request type for you $resource's udpate object, It should have method PUT instead of GET
update: {
method: 'PUT', //<--change it 'PUT' from 'GET'
params: {
op: 'update'
},
isArray: false,
},
Also you had wrong type for delete method, it should have method: 'DELETE' instead of method: 'GET'
I have a service like bellow, how is it possible to call "admins remove" from controller? I know, how to call it without user or admin "scope" (promToRemove.$remove()) but in this situation I am despairing. I also know, how to successfully call get.
angular.module('users').factory('Users', ['$resource', function($resource) {
return {
user: $resource('users/:userId', { userId: '#_id' },
{ get: { method: 'GET' }},
{ update: { method: 'PUT' }}
),
admin: $resource('admin/users/:userId', { userId: '#_id' },
{ get: { method: 'GET' }},
{ update: { method: 'PUT' }},
{ remove: { method: 'DELETE' }}
)
};
}]);
If you want to issue a DELETE command to that API endpoint, try calling the $delete method on that object.
userIWantToDelete.$delete(function() {});
But It will need to have the _id property.
Being relatively new to Angular, I would like some assistance in setting up a new factory that uses ngResource, rather than $http, and that I can pass parameters to.
Based on this example, I have setup a factory as follows:
app.factory('abstractFactory2', function ($resource) {
//var odataUrl = "/odata/ContentType";
return $resource("", {},
{
'getAll': { method: "GET", url: odataUrl, params: {options: "#options" } },
'save': { method: "POST", url: odataUrl, params: { options: "#options" } },
'update': { method: 'PUT', params: { key: "#key" }, url: odataUrl + "(:key)" },
'query': { method: 'GET', params: { key: "#key" }, url: odataUrl + "(:key)" },
'remove': { method: 'DELETE', params: { key: "#key" }, url: odataUrl + "(:key)" }
});
});
I have been able to pass in the value of odataUrl from the controller, but experiencing some odd behavior.
In my controller, the call in my data setup is:
// TODO: pass in odata URL and odata options to factory
var dataSource = new kendo.data.DataSource({
type: "odata",
transport: {
read:
function (options) {
var odataParams = kendo.data.transports["odata"].parameterMap(options.data, "read"); // Object {$inlinecount: "allpages", $format: "json", $top: 10}
(new abstractFactory2.query({ options: odataParams })).$getAll()
.then(function (data) {
return options.success(data);
});
}, . . .
I would like to be able to pass in the odataUrl (as well as other parameters), and the OData options from the function in my controller, and make subsequent appropriate update, create and destroy calls to the factory.
In Fiddler I see that there are two requests to the OData URL, one with partial parameters:
I tried passing in odataUrl = '/odata/contentType' and reading it in the factory withurl: "#odataUrl"` but haven't been able to get that working.
GET /odata/ContentType
GET /odata/ContentType?options=%7B%7D
I would also like to be able to hook in to the .success() and .error() events of the call.
might not the best way but can't you just say url: odataUrl + params