These are chat archives for feroult/yawp

23rd
Aug 2016
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 12:03
Good morning/afternoon
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 13:06
I got a problem
Fernando Ultremare
@feroult
Aug 23 2016 13:08
Morning
What?
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 13:11
Well
I am trying to update the API
to add an admin option
to the accounts
But
everything good
I added a Boolean entry etc
but now when I wanted to modify the existing account
I did a PUT request with { 'isAdmin': false }
Cause it doesn't have it yet
But it responds with a HTTP-500 Error
Fernando Ultremare
@feroult
Aug 23 2016 13:53
Have you redeployed your api?
Can you paste your modelo?
Model
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 13:55
Yeah
Sure
public class Account {

    @Id
    IdRef<Account> id;

    String name;
    String email;

    Boolean isAdmin;
}
while the user data is
{"id":"/accounts/5629499534213120","name":"John","email":"john@doe.com"}
Fernando Ultremare
@feroult
Aug 23 2016 14:51
Oh.. your boolean should be admin
Just Boolean admin;
This is the Java Bean Pattern ... Try like this
No.
Wait.. i'll check on my computer
Can u also paste your Stack trace?
I think it should be fine ti call it isAdmin
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:00
Wait
Sure I'll change it to Boolean admin;
Doesn't matter anyway
There must be another problem
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:09
blob
Fernando Ultremare
@feroult
Aug 23 2016 15:11
how are you doing your request?
can you paste the code here?
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:13
Yeah
sure
var settings = {
  "async": true,
  "url": "http://work-wanders-api.appspot.com/api/accounts/5629499534213120",
  "method": "PUT",
  "headers": {
  },
  "data": "{ 'admin': 'false' }"
}

$.ajax(settings).done(function (response) {
  console.log(response);
});
Fernando Ultremare
@feroult
Aug 23 2016 15:15
I think you should add the json header
and also, remove the '' from false
just 'admin': false
you probably should take a look at the yawp js client
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:15
okay
Sure
Fernando Ultremare
@feroult
Aug 23 2016 15:15
it is all set up
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:16
Yes
it worked!
Oh
So I can use it?
But does it work cross origin?
the newer version
yeah... it is a backend option only
just do yawp('/accounts/12321312').update({ admin: false});
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:18
wait
can't I do
the full URL?
Because the front-end isn't on the same domain as back-end
Fernando Ultremare
@feroult
Aug 23 2016 15:19
look at the guide
you should config yawp
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:19
what guide?
Fernando Ultremare
@feroult
Aug 23 2016 15:19
yawp.config(function (c) {
c.baseUrl('http://your-cors-host.com/api');
});
before you use the yawp client
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:19
OH yeah sorry I missed that
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:20
Also
Say
I want to restrict seeing users
or /api/accounts shall I say
to only users with admin set to true
Fernando Ultremare
@feroult
Aug 23 2016 15:20
the api to go is the Shield api
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:23
Alright I will check everything today if I can haha
Hmmm those shields are interesting
First of all
Fernando Ultremare
@feroult
Aug 23 2016 15:25
yeah :)
give a try to the js client
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:25
Can you please tell me whether I should use JWTs or Cookie sessions with YAWP?
Fernando Ultremare
@feroult
Aug 23 2016 15:26
you should create a filter
to extract the cookie from the request and put on ThreadLocal variable
also, use a JWT library to handle JWT creation
i use the JJWT
i find it good
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:27
JJWT?
Let me check it
Sure then
But how do I process login stuff etc
Like accounts and all that
Fernando Ultremare
@feroult
Aug 23 2016 15:34
yes
if you want, u can use the google login
it works out of the box with appengine
if you to create you own login system
you need to:
1) create a login page
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:35
Yeah?
Fernando Ultremare
@feroult
Aug 23 2016 15:35
2) create an authentication action
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:35
I already got one
I mean
I already got a login page
authentication action?
Fernando Ultremare
@feroult
Aug 23 2016 15:36
where you receive user/password
yeah, /accounts/authenticate for instace
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:36
you mean with @POST("authenticate")?
Fernando Ultremare
@feroult
Aug 23 2016 15:36
yes
already have?
nice
in this action, check the user and password against your database
if if is valid, create the JWT token, write it on a cookie
and redirect to the home page of your app
next step is to have a Filter
that for the protected urls, it extracts the token from the cookie
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:39
Hmmm
But
Fernando Ultremare
@feroult
Aug 23 2016 15:39
check if it is valid yet
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:39
In the login page
How do I pass the login and pass to the authenticate action
Using JSON?
Like
{ 'email': 'john.doe@example.com', 'password': 'password' }
?
Fernando Ultremare
@feroult
Aug 23 2016 15:41
yes
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:41
Hmm
But
in the action code
how do I get it?
Fernando Ultremare
@feroult
Aug 23 2016 15:42
yawp('/accounts').json({ 'email': 'john.doe@example.com', 'password': 'password' }).post('authenticate')
in your action:
@POST
public void authenticate(UserInfo userInfo) {
the UserInfo class should have a email and password
attribute
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:43
Alright
But
Where does the UserInfo come from?
ANd where should the email and pass be stored?
Fernando Ultremare
@feroult
Aug 23 2016 15:43
you create a simple java pojo
to represent it
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:43
Pojo?
Sorry like I told you I am new to Java
Fernando Ultremare
@feroult
Aug 23 2016 15:43
oh a Plain Java Object
ehehe
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:43
Where?
Fernando Ultremare
@feroult
Aug 23 2016 15:44
just an object with two attributes
server side
put in the package you want
to store the user/password
you can persist it in the account model
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:44
Wait
Fernando Ultremare
@feroult
Aug 23 2016 15:44
but remember to encrypt it
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:44
Can I make it in the Account.java ?
How to encrypt it
Fernando Ultremare
@feroult
Aug 23 2016 15:45
you should use a library
there a serveral
there are
you can create a class inside the Account just to represent the user/password login information
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:45
Hmmm
Fernando Ultremare
@feroult
Aug 23 2016 15:46
an Inner class
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:46
What exactly should I encrypt then
Fernando Ultremare
@feroult
Aug 23 2016 15:46
just the password
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:46
yeah of course
But
where should it be stored?
Fernando Ultremare
@feroult
Aug 23 2016 15:46
store it in the Account model
for instance
at the java side, you can
Account a = yawp(Account.class).where("user", "=", user).first();
boolean valid = encrypt(password).equals(a.getPassword())
to save it:
a.setPassword(encrypt(password));
yawp.save(a);
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:48
But then
I will need to hide the password entry from accounts right?
How to do that?
Fernando Ultremare
@feroult
Aug 23 2016 15:48
you can do that using transformers and the shield facade api
but usually i create a separated model to store the password
to make it safer
for instance AccountPassword.class
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:49
Hmm example please?
Sorry for asking too much haha
I hope I can repay this debt some day
Fernando Ultremare
@feroult
Aug 23 2016 15:49
and this endpoint model has no open apis
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:49
so you mean
Fernando Ultremare
@feroult
Aug 23 2016 15:49
no its fine..
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:50
it is only accessible from the server itself?
Fernando Ultremare
@feroult
Aug 23 2016 15:50
yes
you create a Shield to it
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:50
Alright
So
I will now create
Another model
Fernando Ultremare
@feroult
Aug 23 2016 15:50
and put a method public void always() {}l
it will be closed
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:50
called AccountPassword
Fernando Ultremare
@feroult
Aug 23 2016 15:51

public class UserShield extends Shield<User> {

@Override
public void defaults() {
    // TODO Auto-generated method stub
    allow();
}

}

Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:51
Wait
Fernando Ultremare
@feroult
Aug 23 2016 15:51
this is the default shield generated by the scaffold
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:51
Right now
Fernando Ultremare
@feroult
Aug 23 2016 15:51
just remove the allow();
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:51
I got an Account model
Fernando Ultremare
@feroult
Aug 23 2016 15:52
and it will be closed to the outside
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:52
yeah
so now
when I make the AccountPassword endpoint
Fernando Ultremare
@feroult
Aug 23 2016 15:52
go on
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:52
using mvn yawp:endpoint -Dmodel=AccountPassword
The first thing I will do
Is go to the shield and remove the allow(); right?
Fernando Ultremare
@feroult
Aug 23 2016 15:52
right
it will be accessible only server side
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:53
Alright then
So
When I want to make an account
From the signup page
I will POST to /accounts ?
or should I make an action or what
Fernando Ultremare
@feroult
Aug 23 2016 15:54
yes
/accounts/authenticate
your @POST action
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:54
No I am talking about
creating
Fernando Ultremare
@feroult
Aug 23 2016 15:54
to create an account
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:54
but isn't authenticate
for logging in?
Fernando Ultremare
@feroult
Aug 23 2016 15:54
you can POST /account
or create an action to handle further login after creating
you have the option to use a AfterSave hook also
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:55
Hmmm
I want to
make a /accounts/create action
Fernando Ultremare
@feroult
Aug 23 2016 15:55
you have 2 options: post /accounts + AfterSave Hook
post /accounts/create
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:55
that will make an Account entry + make an AccountPassword one
Fernando Ultremare
@feroult
Aug 23 2016 15:55
and create a @POST('create')
action
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:55
Yeah
But
What should I do in that action
to make both an Account and AccountPassword entry
Fernando Ultremare
@feroult
Aug 23 2016 15:56
AccountPassword p = new AccountPassword();
p.setUser(user);
p.setPassword(encrypted_pass);
yawp.save(p);
mark your action as @Atomic
to make sure it will be in a transaction
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:57
Hmmm
So
What about the arguments
Fernando Ultremare
@feroult
Aug 23 2016 15:58
you need to put a java object that represents the JSON you are sending
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:58
Alright then
So
Say
Fernando Ultremare
@feroult
Aug 23 2016 15:58
if its an Account JSON, make it: public void create(Account account);
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:58
I make a CreationInfo
with the attributes of the sign up
and then I will take it as argument
Fernando Ultremare
@feroult
Aug 23 2016 15:59
yeah, its fine
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 15:59
then
I will do
in the code
Account account = new Account();
account.setSomething(); etc
ANd in the end save it
then do the same to AccountPassword
Right?
Fernando Ultremare
@feroult
Aug 23 2016 15:59
you've got it
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 16:00
But
Yeah
Fernando Ultremare
@feroult
Aug 23 2016 16:00
mark that action with @Atomic(cross=true)
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 16:00
How do I even make the setters in Account
?
I mean
I will just do
Fernando Ultremare
@feroult
Aug 23 2016 16:00
are you using an IDE?
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 16:00
functions in the class?
Yeah
IntelliJ IDEA
Fernando Ultremare
@feroult
Aug 23 2016 16:00
eclipse generates getters and setters
IDEA also
I use it
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 16:00
I will check it
Brb haha
Fernando Ultremare
@feroult
Aug 23 2016 16:00
me too :)
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 16:00
Thank you for helping me btw
Fernando Ultremare
@feroult
Aug 23 2016 16:02
no problem... i like it :)
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 16:04
Back, I am trying to make it work now haha
Fernando Ultremare
@feroult
Aug 23 2016 16:39
let me know ;)
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 16:53
Haha yeah
Btw
When I made the AccountPassword
There is the defaults() in the shield
no always()
but in that function there is the allow()
I will remove it right?
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:02
alright
all good
except
how do I use AccountPassword in Account?
how do I import it
Fernando Ultremare
@feroult
Aug 23 2016 17:03
yes
hit CTRL-SPACE over its name
or import package.models.accountpassword.*
intellij should help you importing the package
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:04
hmmm okay
but
I am on Mac
Fernando Ultremare
@feroult
Aug 23 2016 17:04
is it your question? or about querying
on database?
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:04
No
I am asking now
about importing
AccountPassword
to use it
in the action
should I import it using import yawpapp.models.accountpassword.* or import package.models.accountpassword.* ?
Fernando Ultremare
@feroult
Aug 23 2016 17:06
yawpapp
I switched my IDEA to use Eclipse key binds on mac
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:06
alright then
yea?
Fernando Ultremare
@feroult
Aug 23 2016 17:06
i hit CTRL-1 over the AccountPassword
and it suggests the import for me
you can google, importing class intellij shortcut
or something
CTRL-SHIFT-O
organizes imports
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:07
Also
What about ids?
Should I just ignore them
Or what
So
Now
I got it like this
On account.java
Under the Account class
I got this action class
public class CreateAccountAction extends Action<Account> {

    @POST("create")
    public void create(AccountCreationInfo accountInfo) {
        Account account = new Account();
        account.setName(accountInfo.name);
        account.setEmail(accountInfo.email);
        account.setAdmin(false);
        yawp.save(account);

        AccountPassword accountPassword = new AccountPassword();
        accountPassword.setEmail(accountInfo.email);
        accountPassword.setPassword(hash(accountInfo.password));
        yawp.save(accountPassword);
    }

}
Fernando Ultremare
@feroult
Aug 23 2016 17:11
the ids will be generated automatically
no worries
put @Atomic(cross=true)
over @POST
and everything will transactional
this is pretty nice code :)
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:13
Yeah thank you haha
Now?
public class CreateAccountAction extends Action<Account> {

    @Atomic(cross=true)
    @POST("create")
    public void create(AccountCreationInfo accountInfo) {
        Account account = new Account();
        account.setName(accountInfo.name);
        account.setEmail(accountInfo.email);
        account.setAdmin(false);
        yawp.save(account);

        AccountPassword accountPassword = new AccountPassword();
        accountPassword.setEmail(accountInfo.email);
        accountPassword.setPassword(hash(accountInfo.password));
        yawp.save(accountPassword);
    }

}
public class AccountCreationInfo {

    String name;
    String email;
    String password;

}
Fernando Ultremare
@feroult
Aug 23 2016 17:17
nice!
cross means that you are creating transaction across entity groups
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:21
Hmm alright
So I guess creation is done here
what about login?
Fernando Ultremare
@feroult
Aug 23 2016 17:22
@POST("authenticate")
first, forget about encryption
lets focus on JWT
only
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:22
yeah
Fernando Ultremare
@feroult
Aug 23 2016 17:22
then we add encryption
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:22
Sure
public class AccountLoginInfo {

    String email;
    String password;

}
I just made this class
Fernando Ultremare
@feroult
Aug 23 2016 17:24
neat
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:24
    @POST("authenticate")
    public void authenticate(AccountLoginInfo accountLoginInfo) {

    }
What else?
Fernando Ultremare
@feroult
Aug 23 2016 17:24
search the database to get the account password
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:25
how?
Fernando Ultremare
@feroult
Aug 23 2016 17:25
yawp(AccountPassword.class).where("email", "=", accountLoginInfo.email).first();
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:25
Hmmmm
Fernando Ultremare
@feroult
Aug 23 2016 17:25
AccountPassword accountPassword = yawp(AccountPassword.class).where("email", "=", accountLoginInfo.email).first();
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:26
then I will check if accountPassword.password equals accountLoginInfo.password right?
Fernando Ultremare
@feroult
Aug 23 2016 17:26
yeah
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:26
Alright!
then?
Fernando Ultremare
@feroult
Aug 23 2016 17:26
don't forget to test if accountpassowrd is not null
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:26
Hmmm alright
Fernando Ultremare
@feroult
Aug 23 2016 17:26
in this case, this user doesn't exist
then, you'll need to create a JWT token
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:27
alright suppose we did
Fernando Ultremare
@feroult
Aug 23 2016 17:27
and write it on the response cookie
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:27
how?
that's what I am looking for
also what should I do if the user doesn't exist or the login is wrong?
Fernando Ultremare
@feroult
Aug 23 2016 17:28
requestContext.resp().addCookie(cookie);
yawp exposes the request context
through this variable
requestContext.req() => http request, requestContext.resp() => http response
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:29
hmmm
but how do I get requestContext?
Fernando Ultremare
@feroult
Aug 23 2016 17:30
it is there
just type requestContext
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:30
Okay
Fernando Ultremare
@feroult
Aug 23 2016 17:30
is it in the Action hierarchy
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:30
But
Fernando Ultremare
@feroult
Aug 23 2016 17:30
it is
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:30
if the user doesn't exist
or the login is wrong
what should I do?
Fernando Ultremare
@feroult
Aug 23 2016 17:30
requestContext.resp().sendRedirect
or you return a json
and handle it on the client side
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:31
Yeah
I want to return
a JSON
how?
Fernando Ultremare
@feroult
Aug 23 2016 17:31
just return a object from your action
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:31
really?
Fernando Ultremare
@feroult
Aug 23 2016 17:31
yes
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:31
But
the action return type is void
Fernando Ultremare
@feroult
Aug 23 2016 17:31
change it to anything
Object
for instance
or the specific type you want to return
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:32
Hmmm or can I make a class for it?
Sort of like
ErrorInfo
Fernando Ultremare
@feroult
Aug 23 2016 17:32
you can
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:32
wait let me show you
Fernando Ultremare
@feroult
Aug 23 2016 17:32
yes
or AuthenticationResponse
anything
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:33
public class AuthenticationResponse {

    int ResponseCode;

    String ResponseData;

}
Like just an example
Fernando Ultremare
@feroult
Aug 23 2016 17:33
on the client side: yawp('/accounts').json({ user: 'assa', password: 'and'}).post('authenticate').then(function(response) {});
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:33
And make the return type of the action that response
Fernando Ultremare
@feroult
Aug 23 2016 17:33
all yawp js methods return a promise
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:33
yeah
Fernando Ultremare
@feroult
Aug 23 2016 17:33
the promise will have as the first parameter your object
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:33
Alright then
Wow this actually seems much easier than I thought haha
Fernando Ultremare
@feroult
Aug 23 2016 17:34
yeah! :)
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:34
Haha
What about this
I ideally want to be able to get the JWT, store it to our StorageManager (in JavaScript)
Then everytime I want to do something I can just pass it with the JSON data?
Or do you suggest sticking with cookies?
Fernando Ultremare
@feroult
Aug 23 2016 17:36
there a better solution
there is
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:36
which is?
Fernando Ultremare
@feroult
Aug 23 2016 17:36
cookie is sent everything
to send a header
like a bearer
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:37
Hmmm
So what do I use?
Fernando Ultremare
@feroult
Aug 23 2016 17:37
            yawp.config((c) => {
                c.accessToken(baggrToken);
            });
after you get the token
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:38
alright
Fernando Ultremare
@feroult
Aug 23 2016 17:38
set it as an AccessToken
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:38
but
where does that accessToken go?
to a header?
Fernando Ultremare
@feroult
Aug 23 2016 17:39
    String authorization = req.getHeader(AUTHORIZATION_HEADER);
yeah
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:39
Hmmm
Fernando Ultremare
@feroult
Aug 23 2016 17:39
the Authorization header
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:40
What's the value of that AUTHORIZATION_HEADER
just Authorization
heheh
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:40
Haha
Okay then
Now
I am going to finish the authenticate action first
Fernando Ultremare
@feroult
Aug 23 2016 17:40
ok...
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:41
then get to some simple account actions :)
Fernando Ultremare
@feroult
Aug 23 2016 17:41
sure1
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:53
public class CreateAccountAction extends Action<Account> {

    @Atomic(cross=true)
    @POST("create")
    public void create(AccountCreationInfo accountInfo) {
        Account account = new Account();
        account.setName(accountInfo.name);
        account.setEmail(accountInfo.email);
        account.setAdmin(false);
        yawp.save(account);

        AccountPassword accountPassword = new AccountPassword();
        accountPassword.setEmail(accountInfo.email);
        accountPassword.setPassword(hash(accountInfo.password));
        yawp.save(accountPassword);
    }

    @POST("authenticate")
    public void authenticate(AccountLoginInfo accountLoginInfo) {
        AuthenticationResponse authenticationResponse = new AuthenticationResponse();
        AccountPassword accountPassword = yawp(AccountPassword.class).where("email", "=", accountLoginInfo.email).first();

        if (accountPassword == null) {
            authenticationResponse.setResponseCode(AuthenticationCodes.LOGIN_ACCOUNT_NOT_FOUND);
            authenticationResponse.setResponseData(AuthenticationMessages.LOGIN_ACCOUNT_NOT_FOUND);
            return authenticationResponse;
        }

        if (accountPassword.password != accountLoginInfo.password) {
            authenticationResponse.setResponseCode(AuthenticationCodes.LOGIN_WRONG_LOGIN);
            authenticationResponse.setResponseData(AuthenticationMessages.LOGIN_WRONG_LOGIN);
            return authenticationResponse;
        }

        authenticationResponse.setResponseCode(AuthenticationCodes.LOGIN_SUCCESS);
        authenticationResponse.setResponseData(AuthenticationMessages.LOGIN_SUCCESS);
        return authenticationResponse;
    }
}
Now?
Fernando Ultremare
@feroult
Aug 23 2016 17:55
yeah
nice
you could create creator method inside authenticationResponse
to organize
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:56
Huh?
Fernando Ultremare
@feroult
Aug 23 2016 17:56
like: return AuthenticationResponse.WRONG_LOGIN;
but this is just a organising refactory
this is cool and works
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:56
yeah
I just changed btw the return type to the correct one
I will deploy now and test if it works
Fernando Ultremare
@feroult
Aug 23 2016 17:57
then you'll need to add the token in the response
nice
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 17:57
yeah
I will first test
if this works
:D
Also
in the tests
can I have multiple assert calls?
        assertEquals("john.tester@example.com", account.email);
        assertEquals("John Tester", account.name);
        assertEquals(false, account.admin);
Fernando Ultremare
@feroult
Aug 23 2016 18:03
yes
assertFalse is shortcut
for the last
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 18:03
Hmmm alright
so assertFalse(account.admin); ?
Fernando Ultremare
@feroult
Aug 23 2016 18:04
yes
you will need to import
Assert.assertFalse
like the assertEquals
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 18:05
Yea
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile (default-compile) on project yawpapp: Compilation failure: Compilation failure:
[ERROR] /Users/walidsabihi/ww-appspot/yawpapp/src/main/java/yawpapp/models/account/Account.java:[95,7] error: class CreateAccountAction is public, should be declared in a file named CreateAccountAction.java
[ERROR] /Users/walidsabihi/ww-appspot/yawpapp/src/main/java/yawpapp/models/account/Account.java:[95,41] error: cannot find symbol
[ERROR] symbol: class Action
[ERROR] /Users/walidsabihi/ww-appspot/yawpapp/src/main/java/yawpapp/models/account/Account.java:[97,5] error: cannot find symbol
[ERROR] symbol:   class Atomic
[ERROR] location: class CreateAccountAction
[ERROR] /Users/walidsabihi/ww-appspot/yawpapp/src/main/java/yawpapp/models/account/Account.java:[98,5] error: cannot find symbol
[ERROR] symbol:   class POST
[ERROR] location: class CreateAccountAction
[ERROR] /Users/walidsabihi/ww-appspot/yawpapp/src/main/java/yawpapp/models/account/Account.java:[112,5] error: cannot find symbol
[ERROR] symbol:   class POST
[ERROR] location: class CreateAccountAction
[ERROR] /Users/walidsabihi/ww-appspot/yawpapp/src/main/java/yawpapp/models/account/Account.java:[104,8] error: cannot find symbol
[ERROR] symbol:   variable yawp
[ERROR] location: class CreateAccountAction
[ERROR] /Users/walidsabihi/ww-appspot/yawpapp/src/main/java/yawpapp/models/account/Account.java:[109,8] error: cannot find symbol
[ERROR] symbol:   variable yawp
[ERROR] location: class CreateAccountAction
[ERROR] /Users/walidsabihi/ww-appspot/yawpapp/src/main/java/yawpapp/models/account/Account.java:[115,42] error: cannot find symbol
[ERROR] symbol:   method yawp(Class<AccountPassword>)
[ERROR] location: class CreateAccountAction
[ERROR] /Users/walidsabihi/ww-appspot/yawpapp/src/main/java/yawpapp/models/account/Account.java:[123,27] error: password is not public in AccountPassword; cannot be accessed from outside package
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
Should I put it in it's own file?
Fernando Ultremare
@feroult
Aug 23 2016 18:09
yeah
put each class in one file
with the same name
of the class
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 18:09
Hmmm but will the compiler still compile it too?
or is there a mvn command to make an action?
Fernando Ultremare
@feroult
Aug 23 2016 18:16
everything is in the same file right now?
can you paste it?
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 18:17
Wait
Yeah
package yawpapp.models.account;

import io.yawp.repository.IdRef;
import io.yawp.repository.annotations.Endpoint;
import io.yawp.repository.annotations.Id;

import yawpapp.models.accountpassword.*;

class AccountCreationInfo {

    String name;
    String email;
    String password;

}

class AccountLoginInfo {

    String email;
    String password;

}

class AuthenticationResponse {

    int ResponseCode;

    String ResponseData;

    public int getResponseCode() {
        return ResponseCode;
    }

    public void setResponseCode(int responseCode) {
        ResponseCode = responseCode;
    }

    public String getResponseData() {
        return ResponseData;
    }

    public void setResponseData(String responseData) {
        ResponseData = responseData;
    }
}

interface AuthenticationCodes {
    public static final int LOGIN_ACCOUNT_NOT_FOUND = 1012;
    public static final int LOGIN_WRONG_LOGIN = 1013;
    public static final int LOGIN_SUCCESS = 1011;
}

interface AuthenticationMessages {
    public static final String LOGIN_ACCOUNT_NOT_FOUND = "Account not found";
    public static final String LOGIN_WRONG_LOGIN = "Wrong password";
    public static final String LOGIN_SUCCESS = "Login success";
}

@Endpoint(path = "/accounts")
public class Account {

    @Id
    IdRef<Account> id;

    String name;
    String email;

    Boolean admin;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Boolean getAdmin() {
        return admin;
    }

    public void setAdmin(Boolean admin) {
        this.admin = admin;
    }
}

public class CreateAccountAction extends Action<Account> {

    @Atomic(cross=true)
    @POST("create")
    public void create(AccountCreationInfo accountInfo) {
        Account account = new Account();
        account.setName(accountInfo.name);
        account.setEmail(accountInfo.email);
        account.setAdmin(false);
        yawp.save(account);

        AccountPassword accountPassword = new AccountPassword();
        accountPassword.setEmail(accountInfo.email);
        accountPassword.setPassword(accountInfo.password);
        yawp.save(accountPassword);
    }

    @POST("authenticate")
    public AuthenticationResponse authenticate(AccountLoginInfo accountLoginInfo) {
        AuthenticationResponse authenticationResponse = new AuthenticationResponse();
        AccountPassword accountPassword = yawp(AccountPassword.class).where("email", "=", accountLoginInfo.email).first();

        if (accountPassword == null) {
            authenticationResponse.setResponseCode(AuthenticationCodes.LOGIN_ACCOUNT_NOT_FOUND);
            authenticationResponse.setResponseData(AuthenticationMessages.LOGIN_ACCOUNT_NOT_FOUND);
            return authenticationResponse;
        }

        if (accountPassword.password != accountLoginInfo.password) {
            authenticationResponse.setResponseCode(AuthenticationCodes.LOGIN_WRONG_LOGIN);
            authenticationResponse.setResponseData(AuthenticationMessages.LOGIN_WRONG_LOGIN);
            return authenticationResponse;
        }

        authenticationResponse.setResponseCode(AuthenticationCodes.LOGIN_SUCCESS);
        authenticationResponse.setResponseData(AuthenticationMessages.LOGIN_SUCCESS);
        return authenticationResponse;
    }
}
Fernando Ultremare
@feroult
Aug 23 2016 18:18
yeah
in Java you can have two classes in the same file
but one class should be inside the other
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 18:18
hmmm so what should I do
Fernando Ultremare
@feroult
Aug 23 2016 18:18
otherwise you need separate files
mvn clean install
compiles and handle everything
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 18:19
so
what should I do now
Fernando Ultremare
@feroult
Aug 23 2016 18:19
you'll just need to import classes if they are in different packages
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 18:19
wait
right now
what do I do
Fernando Ultremare
@feroult
Aug 23 2016 18:19
I'd split the models
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 18:19
You mean classes?
Fernando Ultremare
@feroult
Aug 23 2016 18:19
Account and AccountPassword
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 18:19
The models Account and AccountPassword are already split
Fernando Ultremare
@feroult
Aug 23 2016 18:20
in different files
and also the other clsasses
LoginInfo
etc
to start
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 18:20
Yeah but
say
Fernando Ultremare
@feroult
Aug 23 2016 18:20
create one file for each
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 18:20
I split them
each class on a file
Fernando Ultremare
@feroult
Aug 23 2016 18:20
yeah
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 18:20
but how do I use them?
Fernando Ultremare
@feroult
Aug 23 2016 18:20
if they are in the same directory
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 18:20
Or will the compiler automatically use them
Fernando Ultremare
@feroult
Aug 23 2016 18:20
they will work as if they were in the same file
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 18:20
Really?
Fernando Ultremare
@feroult
Aug 23 2016 18:20
yep
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 18:21
Alright let me try
But
should I do in the top the package statement on each file?
also what about annotations?
should they be imported too?
Fernando Ultremare
@feroult
Aug 23 2016 18:24
yeah
everything
hit CTRL SPACE
over the red(errors) words
IntelliJ should ask you to import
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 18:24
Well the thing is
I guess my IDE isn;t correctly set up
It shows no errors
Fernando Ultremare
@feroult
Aug 23 2016 18:24
hnn
how did you open the project?
you can close the project
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 18:25
Okay
Fernando Ultremare
@feroult
Aug 23 2016 18:25
remve .idea and *.iml files
go IntelliJ, open project >> select the folder
it will ask you to load configuration on a right top toast message
click to import
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 18:26
Hmmm
There is no open project
only new project, import project and open
Fernando Ultremare
@feroult
Aug 23 2016 18:26
Import
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 18:28
Hmmm
It asks
Whether to create project from existing sources
or
Import project from external model
under the second option there is Eclipse/Gradle/Maven (list)
Fernando Ultremare
@feroult
Aug 23 2016 18:29
maven
ops
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 18:29
?
Fernando Ultremare
@feroult
Aug 23 2016 18:29
try maven
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 18:29
ok
blob
Fernando Ultremare
@feroult
Aug 23 2016 18:31
next
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 18:31
ok
blob
Fernando Ultremare
@feroult
Aug 23 2016 18:32
next
this is the right path
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 18:38
Alright
all set up
I fixed it all
testing if it deploys
Fernando Ultremare
@feroult
Aug 23 2016 18:38
mvn clean install works?
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 18:40
idk
didn't do it
I guess not needed?
anyway it deployed succesfully
Also
Is there any way
to dump the data?
The whole API data
like the whole database
Fernando Ultremare
@feroult
Aug 23 2016 18:43
you should use one datastore admin tool
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 18:43
?
Fernando Ultremare
@feroult
Aug 23 2016 18:43
at the console.developer.google.com
there is a datastore admin tool
you can backup
and stuff
console.cloud.google.com
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 18:43
hmmm let me see
Fernando Ultremare
@feroult
Aug 23 2016 18:44
i use to use this tool: https://github.com/murer/dsopz
it is a python datastore
admin tool
that does it...
but it is not working... google changed the beta api
to connect directly to datastore..
you can access /accounts
to get all accounts thought yawp
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 18:45
yeah
Nevermind I actually can access the databases from the dashboard
Also
When I log in with correct pass it still says wrong password
Fernando Ultremare
@feroult
Aug 23 2016 18:46
run it on your local devserver
you can debug or put some logs
to see what is happening
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 18:46
hmmm
Fernando Ultremare
@feroult
Aug 23 2016 18:47
mvn yawp:devserver
or mvn appengine:devserver
will bring your api to localhost:8080
to test locally
Walid Nawfal Sabihi
@LvlAndFarm
Aug 23 2016 18:48
wait
sure
Oh well I guess I will check it tomorrow
I have to go now!
Talk to you later
And thank you for helping me haha
Fernando Ultremare
@feroult
Aug 23 2016 18:51
ok then! good luck! :)