loopback icon indicating copy to clipboard operation
loopback copied to clipboard

How can i use ttl attribute of property ?

Open bluestaralone opened this issue 12 years ago • 20 comments

"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.

bluestaralone avatar Nov 04 '14 02:11 bluestaralone

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 avatar Nov 05 '14 02:11 Spark-star

@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
      }
    }

bluestaralone avatar Nov 05 '14 03:11 bluestaralone

@raymondfeng : what do you think about it ?

bluestaralone avatar Nov 05 '14 03:11 bluestaralone

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

raymondfeng avatar Nov 05 '14 04:11 raymondfeng

@raymondfeng Yes, but it doesn't work.

bluestaralone avatar Nov 05 '14 04:11 bluestaralone

@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 avatar Nov 05 '14 06:11 Spark-star

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

raymondfeng avatar Nov 05 '14 17:11 raymondfeng

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.

Spark-star avatar Nov 06 '14 01:11 Spark-star

@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.

bluestaralone avatar Nov 11 '14 02:11 bluestaralone

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 avatar Nov 11 '14 02:11 Spark-star

@Spark-star Is this issue good to close?

superkhau avatar Feb 26 '15 03:02 superkhau

Is this ticket good to close?

Amir-61 avatar Dec 13 '15 17:12 Amir-61

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

Thanks!

Amir-61 avatar Dec 15 '15 20:12 Amir-61

Boy you guys are useless

redevill avatar Jan 23 '18 23:01 redevill

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})

redevill avatar Jan 23 '18 23:01 redevill

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

redevill avatar Jan 23 '18 23:01 redevill

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 avatar Jan 23 '18 23:01 redevill

@redevill

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

oderayi avatar May 02 '18 14:05 oderayi

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!

lane-tang avatar Sep 13 '18 02:09 lane-tang

@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

heshamelmasry77 avatar Feb 18 '19 17:02 heshamelmasry77