Skip to content

Commit 11ebff2

Browse files
Merge pull request #253 from sandeep-vedam/pin-context
Pin context
2 parents f8cbc84 + cfac73f commit 11ebff2

File tree

2 files changed

+48
-18
lines changed

2 files changed

+48
-18
lines changed

docs/plugins/pin.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,17 @@ Pin.hide()
3636

3737
Sends a Pin code to the middleware layer for verification. If the code is correct, the STB will be unlocked.
3838

39+
The `submit` method accepts `pin` and `context` as it's arguments. The context argument indicates in with what purpsose the Pin dialog is being used. The accepted values van be either `parental` or `purchase`. `context` defaults to `purchase`, but it's recommended to explicitly specify the context
40+
3941
The `submit` method is automatically invoked when you are using the built-in **Pin** dialog. Use this method for sending the Pin code *only* if you are making a fully custom **Pin** dialog in your App.
4042

4143
```js
42-
Pin.submit('0000')
44+
Pin.submit('0000', context)
4345
.then(() => console.log('Unlocked!'))
4446
.catch(e => console.log('Pin error', e))
4547
```
4648

47-
The `submit` method returns a *Promise*. If the supplied Pin code is correct, the Promise resolves with `true` and the STB will be unlocked. If the Pin code is wrong, the Promise resolves with `false`.
49+
The `submit` method returns a *Promise*. If the supplied Pin code is correct (and context is valid), the Promise resolves with `true` and the STB will be unlocked. If the Pin code or context are wrong, the Promise resolves with `false`.
4850

4951
If the middleware is unable to unlock the STB, the Promise is *rejected* (with an optional error message).
5052

@@ -55,7 +57,7 @@ During development, the default Pin code is `0000`. Optionally, you can overwri
5557
Checks if the STB is currently *unlocked*.
5658

5759
```js
58-
Pin.unlocked()
60+
Pin.unlocked(context)
5961
.then(
6062
unlocked => unlocked === true ?
6163
console.log('STB is unlocked') :
@@ -71,7 +73,7 @@ If the middleware is unable to retrieve the current state, the Promise is *rejec
7173
Checks if the STB is currently *locked*.
7274

7375
```js
74-
Pin.locked()
76+
Pin.locked(context)
7577
.then(
7678
locked => locked === true ?
7779
console.log('STB is locked') :

src/Pin/index.js

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
import Settings from '../Settings'
2121
import PinDialog from './dialog'
2222
import { ApplicationInstance } from '../Launch'
23+
import { Log } from '../../index'
2324

2425
// only used during local development
2526
let unlocked = false
27+
const contextItems = ['purchase', 'parental']
2628

27-
let submit = pin => {
29+
let submit = (pin, context) => {
2830
return new Promise((resolve, reject) => {
2931
if (pin.toString() === Settings.get('platform', 'pin', '0000').toString()) {
3032
unlocked = true
@@ -35,7 +37,7 @@ let submit = pin => {
3537
})
3638
}
3739

38-
let check = () => {
40+
let check = context => {
3941
return new Promise(resolve => {
4042
resolve(unlocked)
4143
})
@@ -52,6 +54,17 @@ export const initPin = config => {
5254

5355
let pinDialog = null
5456

57+
const contextCheck = context => {
58+
if (context === undefined) {
59+
Log.info('Please provide context explicitly')
60+
return contextItems[0]
61+
} else if (!contextItems.includes(context)) {
62+
Log.warn('Incorrect context provided')
63+
return false
64+
}
65+
return context
66+
}
67+
5568
// Public API
5669
export default {
5770
show() {
@@ -73,34 +86,49 @@ export default {
7386
)
7487
pinDialog = null
7588
},
76-
submit(pin) {
89+
submit(pin, context) {
7790
return new Promise((resolve, reject) => {
7891
try {
79-
submit(pin)
80-
.then(resolve)
81-
.catch(reject)
92+
context = contextCheck(context)
93+
if (context) {
94+
submit(pin, context)
95+
.then(resolve)
96+
.catch(reject)
97+
} else {
98+
reject('Incorrect Context provided')
99+
}
82100
} catch (e) {
83101
reject(e)
84102
}
85103
})
86104
},
87-
unlocked() {
105+
unlocked(context) {
88106
return new Promise((resolve, reject) => {
89107
try {
90-
check()
91-
.then(resolve)
92-
.catch(reject)
108+
context = contextCheck(context)
109+
if (context) {
110+
check(context)
111+
.then(resolve)
112+
.catch(reject)
113+
} else {
114+
reject('Incorrect Context provided')
115+
}
93116
} catch (e) {
94117
reject(e)
95118
}
96119
})
97120
},
98-
locked() {
121+
locked(context) {
99122
return new Promise((resolve, reject) => {
100123
try {
101-
check()
102-
.then(unlocked => resolve(!!!unlocked))
103-
.catch(reject)
124+
context = contextCheck(context)
125+
if (context) {
126+
check(context)
127+
.then(unlocked => resolve(!!!unlocked))
128+
.catch(reject)
129+
} else {
130+
reject('Incorrect Context provided')
131+
}
104132
} catch (e) {
105133
reject(e)
106134
}

0 commit comments

Comments
 (0)