Hello, I’m trying to implement my custom register service based on my custom user auth but I’m having problems when I send a new registration request.
It seems it always overwrites my first user in the db (the id isn’t incremented).
I define my authentication system like this:
Plugins.Add(
new AuthFeature(() => new CustomUserSession(),
new IAuthProvider[]
{
new CustomAuthProvider(),
}
));
this.RegisterService<CustomRegisterService>("/register");
this.RegisterAs<RegistrationValidator, IValidator<Register>>();
container.Register<IUserAuthRepository>(c => new OrmLiteAuthRepository(c.Resolve<IDbConnectionFactory>()));
var authRepo = (OrmLiteAuthRepository)container.Resolve<IUserAuthRepository>();
authRepo.InitSchema();
Here is my custom registration service class (I basically overwrited, with the “new” keyword, the Post and Put methods without change them for now):
[DefaultRequest(typeof(Register))]
public class CustomRegisterService : RegisterService<CustomUserAuth>
{
public new object Put(Register request)
{
return Post(request);
}
public new object Post(Register request)
{
if (HostContext.GlobalRequestFilters == null
|| !HostContext.GlobalRequestFilters.Contains(ValidationFilters.RequestFilter)) //Already gets run
{
if (RegistrationValidator != null)
{
RegistrationValidator.ValidateAndThrow(request, ApplyTo.Post);
}
}
var userAuthRepo = AuthRepo.AsUserAuthRepository(GetResolver());
if (ValidateFn != null)
{
var validateResponse = ValidateFn(this, HttpMethods.Post, request);
if (validateResponse != null)
return validateResponse;
}
RegisterResponse response = null;
var session = this.GetSession();
var newUserAuth = ToUserAuth(request);
var existingUser = userAuthRepo.GetUserAuth(session, null);
var registerNewUser = existingUser == null;
var user = registerNewUser
? userAuthRepo.CreateUserAuth(newUserAuth, request.Password)
: userAuthRepo.UpdateUserAuth(existingUser, newUserAuth, request.Password);
if (request.AutoLogin.GetValueOrDefault())
{
using (var authService = base.ResolveService<AuthenticateService>())
{
var authResponse = authService.Post(
new Authenticate
{
provider = CredentialsAuthProvider.Name,
UserName = request.UserName ?? request.Email,
Password = request.Password,
Continue = request.Continue
});
if (authResponse is IHttpError)
throw (Exception)authResponse;
var typedResponse = authResponse as AuthenticateResponse;
if (typedResponse != null)
{
response = new RegisterResponse
{
SessionId = typedResponse.SessionId,
UserName = typedResponse.UserName,
ReferrerUrl = typedResponse.ReferrerUrl,
UserId = user.Id.ToString(CultureInfo.InvariantCulture),
};
}
}
}
if (registerNewUser)
{
session = this.GetSession();
session.OnRegistered(this.Request, session, this);
if (AuthEvents != null)
AuthEvents.OnRegistered(this.Request, session, this);
}
if (response == null)
{
response = new RegisterResponse
{
UserId = user.Id.ToString(CultureInfo.InvariantCulture),
ReferrerUrl = request.Continue
};
}
var isHtml = Request.ResponseContentType.MatchesContentType(MimeTypes.Html);
if (isHtml)
{
if (string.IsNullOrEmpty(request.Continue))
return response;
return new HttpResult(response)
{
Location = request.Continue
};
}
return response;
}
}
here is my POST request:
api/register?UserName=test&DisplayName=TestUser&Email=test@test.com&Password=test
I don’t pass any Id
so I was hoping it would be autoincremented by default… I’m wrong?