Loopback: How can i use ttl attribute of property ?

Created on 4 Nov 2014  Â·  20Comments  Â·  Source: strongloop/loopback

"ttl": {
      "type": "number",
      "ttl": true,
      "default": 5,
      "description": "time to live in seconds (5 seconds by default)"
    },

As you see in the JSON i set ttl for current model is 5 seconds but it wasn't deleted automatically.

Most helpful comment

Alternately: You can have a common model that overrides the login function.
*.js file
module.exports = Employees => {
Employees.on('dataSourceAttached', model => {
const login = Employees.login;
Employees.login = function (credentials, include, cb) {
credentials.ttl = MyCustomTTLValue;
return login.call(this, credentials, include, cb);
};
});
};

*.json specify the user as base
{
"name": "employees",
"base": "User", ... etc

All 20 comments

Thanks for your answer
I tried it ,but it doesn't work .
I set the ttl like this,

"ttl": {
"type": "number",
"ttl": true,
"default": 5,
"description": "time to live in seconds (5 seconds by default)"
},

but the response 'token's 'ttl' always be '129600'.

the loopback I used is version 2.7.0.

2014-11-04 10:23 GMT+08:00 SangNT [email protected]:

"ttl": {
"type": "number",
"ttl": true,
"default": 5,
"description": "time to live in seconds (5 seconds by default)"
},

As you see in the JSON i set ttl for current model is 5 seconds but it
wasn't deleted automatically.

—
Reply to this email directly or view it on GitHub
https://github.com/strongloop/loopback/issues/733.

@Spark-star:
I think you should check on user.js on node_modules/loopback/common/models/user.js and search that number you can see that why 'ttl' always be '129600'.

But i have solution for this by set indexes in model.json you can use ttl as it is.

    "ttl_idx": { 
      "keys": {
        "created": 1 
      }, 
      "options": {
        "expireAfterSeconds": 900
      }
    }

@raymondfeng : what do you think about it ?

What do you try to do? Setting TTL for access token so that expired tokens will be deleted automatically?

@raymondfeng
Yes, but it doesn't work.

@raymondfeng https://github.com/raymondfeng , I want the token valid time
more than '129600'(two weeks).
How can I do ?
I set the 'ttl' to '31449600'(52 weeks) in my model which extended from
user model, but it is overrided by something.
Is it a bug?

2014-11-05 12:46 GMT+08:00 SangNT [email protected]:

Yes, but it doesn't work.

—
Reply to this email directly or view it on GitHub
https://github.com/strongloop/loopback/issues/733#issuecomment-61759394.

@Spark-star Can you show us the model json?

This is my app 'Account.json'.
{ "name": "Account",
"plural": "accounts",
"base": "User",
"Model":
{
"Master": "Master",
"Servant": "Servant",
"AccessToken": "AccessToken",
"Role": "Role"
},
"ttl": 31449600,
"properties": {
"cellphone":
{
"type": "string",
"required": true
}
},
"validations": [],
"relations":
{
"master":
{ "type": "hasOne",
"model": "Master"
},
"servant":
{ "type": "hasOne",
"model": "Servant"
}
},
"acls": [ ],
"methods": []
}

2014-11-06 1:10 GMT+08:00 Raymond Feng [email protected]:

@Spark-star https://github.com/Spark-star Can you show us the model
json?

—
Reply to this email directly or view it on GitHub
https://github.com/strongloop/loopback/issues/733#issuecomment-61844217.

@raymondfeng
This is your question:

What do you try to do? Setting TTL for access token so that expired tokens will be deleted automatically?

This is my answer:

Yes, but it doesn't work. (not automatically deleted)

Please focus on my problem.

@Spark-star
Please create another isssue for your problem.

I resolved my problem through setting ttl dynamic in my model.js.
Thx

2014-11-11 10:43 GMT+08:00 SangNT [email protected]:

@raymondfeng https://github.com/raymondfeng
This is your question:

What do you try to do? Setting TTL for access token so that expired tokens will be deleted automatically?

This is my answer:

Yes, but it doesn't work. (not automatically deleted)

Please focus on my problem.

@Spark-star https://github.com/Spark-star
Please create another isssue for your problem.

—
Reply to this email directly or view it on GitHub
https://github.com/strongloop/loopback/issues/733#issuecomment-62493750.

@Spark-star Is this issue good to close?

Is this ticket good to close?

I'm closing this ticket due to inactivity for months. Please feel free to open it if you run into the same issue.

Thanks!

Boy you guys are useless

Quite a legit problem -> to be able to set the ttl to a value of your choosing, and you have no answer?

On your client side you can Hack it in: this.http.post('api/user/login', {email: email, password: password})

Posted too soon... but anyway - you can add it to the credential json object - as a hack.

Alternately: You can have a common model that overrides the login function.
*.js file
module.exports = Employees => {
Employees.on('dataSourceAttached', model => {
const login = Employees.login;
Employees.login = function (credentials, include, cb) {
credentials.ttl = MyCustomTTLValue;
return login.call(this, credentials, include, cb);
};
});
};

*.json specify the user as base
{
"name": "employees",
"base": "User", ... etc

@redevill

You're doing a better job than the loopback team. Thanks man! Works for me!

just add this line of code:
Account.settings.ttl = MyCustomTTLValue;
in the you extension file [ account.js ] like this:

module.exports = function(Account) {  
  Account.settings.ttl = MyCustomTTLValue;   
  // ...   
}

that works for me!

@behindtheshadow and how your .json looks like ?
for example what i am trying to approach is to have a ttl or whatever property and it expire after certian time.
i added as you said : in my .js

'use strict';

module.exports = function(Room) {
  Room.settings.ttl = 5;
};

and on my .json i added

"ttl": {
      "type": "number",
      "ttl": true,
      "default": 5,
      "description": "time to live in seconds (5 seconds by default)"
    }

but nothing happened

Was this page helpful?
0 / 5 - 0 ratings